Making a function out of a script
Mostrar comentarios más antiguos
Hallo Matlab Community,
This is my Code:
variablen1 = who('*_2017_05_01','A123_18650M1A_110_2017_04_30','A123_18650M1A_110_2017_04_29');
Linedischarge=[9,16,23,30,37];
Ergebnis.Ah=nan(length(variablen1),length(Linedischarge));
Ergebnis.Wh=nan(length(variablen1),length(Linedischarge));
Ergebnis.P=nan(length(variablen1),length(Linedischarge));
for k1=1:length(variablen1)
tmp_struct = eval(variablen1{k1});
messdaten= tmp_struct.Data;
for k2 =1:length(Linedischarge)
templine=Linedischarge(k2);
reihe =(messdaten.State == 2)&(messdaten.Line==templine);
Ergebnis.Ah(k1,k2)= messdaten.Ah_step(reihe);
Ergebnis.Wh(k1,k2)= messdaten.Wh_step(reihe);
Ergebnis.P(k1,k2)= messdaten.P(reihe);
end
f1=figure;
ax1=gca;
grid on; hold all;
title('A123, Panasonic und Samsung');
ylabel('Leistung in W');
xlabel('Kapazität in Ah');
f2=figure;
ax2=gca;
grid on; hold all;
title('A123, Panasonic und Samsung');
ylabel('Leistung in W');
xlabel('Energie in Wh');
if true
% code
end
for k1 = 1:length(variablen1)
plot(ax1,-Ergebnis.Ah(k1,:),-Ergebnis.P(k1,:),'Marker','x')
plot(ax2,-Ergebnis.Wh(k1,:),-Ergebnis.P(k1,:),'Marker','x')
end
if true
% code
end
legend(ax1,variablen1, 'Interpreter', 'none','Location','North');
legend(ax2,variablen1, 'Interpreter', 'none','Location','North');
end
At the beginning of this code I declare a struct called Ergebnis and then I fill this struct with variablen 1 and with linedischarge. I want to make this part of the script as function. Can somebody help me please?
Best, Mike
4 comentarios
Michael Szostak
el 5 de Mayo de 2017
Editada: KSSV
el 5 de Mayo de 2017
@Michael Szostak: do you have a question? What, exactly, do you want?
You used eval, which make code obfuscated and hard to debug, and now your code is obfuscated and hard to debug. The best advice your will get is to avoid using eval, and import your data correctly into one array or structure. Then you code will be a lot simpler:
PS: Beginners think that eval solves all of their problems. It doesn't. It just creates other problems. Read the link I gave and import that data in such a way that you do not need eval. Most likey you would need to do something as simple as load-ing into a structure:
S = load(...);
but as you do not give any details about how you get those variables into your workspace, or even ask a question, it is hard to give any specific recommendation.
Michael Szostak
el 8 de Mayo de 2017
"so the script I wrote in the beginning is working. I only want to have out of the script a function"
Sure, maybe that script works. But just because a script "works" does not mean that it is written in a way that makes it easy or worthwhile to convert it into a function. There is no general requirement that an arbitrary "working" script can be turned efficiently into a function. And your script/function, by using eval and evalin (being two methods that make code very inefficient, slow, hard to read, buggy, hard to debug, obfuscated,...), will not make an efficient function: it will be hard to make it work properly.
"Basiclly I want to get out of this script a function"
Well, if you are happy to keep writing inefficient, buggy code than you already using all of the functions that you need. But I note that your code is most likely not working (aka "buggy"), otherwise you would not be asking here for advice. It should not be a surprise when people try to help you to write better code (some might call this "advice", which of course you are welcome to ignore).
"Can you help me please?"
Start by importing your data into one variable, rather than lots of specially named variables, then you can trivially process all of the data using a loop. Good luck!
Respuestas (1)
Jan
el 8 de Mayo de 2017
0 votos
Creating variables with names like "A_2017_05_01" is a really bad idea. Hiding important information in the names of the variables is too complicated and requires even miore complicated method to access then later. Don't do this.
The topic is discussed daily. Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
1 comentario
Jan
el 9 de Mayo de 2017
Hi Michael, I can reconsider that you have expected a different answer. But the problem of creating variables dynamically by eval is serious. Your script migth work now, but it is horrible to maintain or expand it. Using arrays is much faster, cleaner and easier to maintain.
In your case the dynamic access to the list of variables by who('*_2017_05_01') is the main problem: You cannot move this into the function, because the variables exist in the caller only. Another indirection using evalin to evaluate the who command in the caller and obtaining the variables remote controlled by further evalin's is a really bad idea, because the complexitiy of the code will explode and the performance will break down. The function could not be called from anywhere, but requires the caller to use certain names of variables. This is the opposite of the idea of using functions.
Therefore I'm convinced this is the best time to re-design your code. With the eval the current code ran into a dead end - and this is typical for eval codes.
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!