Making a function out of a script

조회 수: 3 (최근 30일)
Michael Szostak
Michael Szostak 2017년 5월 5일
댓글: Jan 2017년 5월 9일
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
Michael Szostak
Michael Szostak 2017년 5월 8일
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 2017년 5월 8일
편집: Stephen23 2017년 5월 8일
"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!

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2017년 5월 8일
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
Jan
Jan 2017년 5월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by