Making a function out of a script

3 Ansichten (letzte 30 Tage)
Michael Szostak
Michael Szostak am 5 Mai 2017
Kommentiert: Jan am 9 Mai 2017
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 Kommentare
Michael Szostak
Michael Szostak am 8 Mai 2017
Hi, so the script I wrote in the beginning is working. I only want to have out of the script a function. So I will have the beginning with this code:
variablen1 = who('*_2017_05_01','A123_18650M1A_110_2017_04_30','A123_18650M1A_110_2017_04_29'); Linedischarge=[9,16,23,30,37];
Then I want to call the function which calculates the matrix and afterwards I will plot the values which are calculated by the function in the script again. Basiclly I want to get out of this script a function, which fills in the matrix and I don´t know how to do it? Can you help me please?
Stephen23
Stephen23 am 8 Mai 2017
Bearbeitet: Stephen23 am 8 Mai 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!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 8 Mai 2017
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.
  1 Kommentar
Jan
Jan am 9 Mai 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by