How to Save data generated during a function execution

I am working on automation project, while running my script I call a function with some input argument which returns a result to me, but in the meanwhile the function also creates snp file of the schematic, I want to store that data but I cant ask it as output argument from the function. Is there any way to store data created within the function, Please let me know.

Antworten (1)

Image Analyst
Image Analyst am 13 Aug. 2022

0 Stimmen

I don't know why you say that. You can save data created inside the function in a variety of ways and formats. Or, you can also return the variables to the calling function and save them there instead. Or you can actually do both if you want.
Perhaps if you shared a simplified snippet of what you want to do, we could guide you. And tell us
  1. what variable(s) you want to save
  2. where you want to save it (in the script or the function)
  3. what format do you want to save in (text file, spreadsheet, .mat file, image file, sound file, etc.)

3 Kommentare

ok so this is what my code there is an objective function cost that I want to optimize, the cost function is called n number of times,
for k = 1:N
cost = @(x)compute_cost(x,x_name,IND,CAP,snp,port,init_table,freq,freqq,z0,perm_nodes,mux_nodes,k);
nonlcon = @(x)compute_nonlcon(x,x_name,IND,CAP,snp,port,init_table,freq,freqq,z0,perm_nodes,mux_nodes,k,target_gain);
% 'Algorithm', 'sqp'
options = optimoptions(@fmincon,...  
'Algorithm', 'sqp', ...
'TolFun', 1e-3, ...    
'TolX', 1e-3, ...   
'TolCon', 1e-3, ...  
'MaxFunEvals', 20000, ...  
'Display', 'off', ...   
'MaxIterations', 30, ...  
'PlotFcn', {'optimplotfvalconstr', 'optimplotconstrviolation'});
[x_opt,fval] = fmincon(cost,x,[],[],[],[],V_table.lb,V_table.ub, nonlcon,options);
x_opt_store(:,k) = x_opt;
fval_store(1,k) = fval;
end
under this cost function I call other function that generate smatrix(data) the function look like this
function [cost] = compute_cost(x,x_name,IND,CAP,snp,port,k)
x_name_unopt = init_table.Row(~ismember(init_table.Row,x_name));
value_unopt = init_table{x_name_unopt,'ele_init_val'};
ckt_renormspara = compute_circuit(IND,CAP,snp,port,k);
%calling sParamClass for one schematic
news_norm = sParamClass('s',ckt_renormspara,'freq',freq,'Z0',z0); % this data I want to save
and the smatrix looks like this and this is being creted within the function so I want to know how can I save all n smatrix that is being created with iteration of my cost function.
val(:,:,1) =
Columns 1 through 2
0.028830733972448 + 0.250347708621344i 0.217095871317935 - 0.269001313860388i
0.217095871317935 - 0.269001313860388i -0.404457099004175 - 0.679893826962543i
0.247680627165406 - 0.266531263952234i 0.108888570419545 + 0.185844471726056i
0.241196455059012 - 0.228048881621474i 0.120776872573937 + 0.126111908574153i
0.256961472320009 - 0.276649641863618i 0.146396214605169 + 0.134241260584520i
0.133261250722424 - 0.366115587638408i 0.193042562906943 + 0.068867826288139i
0.241196455059012 - 0.228048881621474i 0.120776872573937 + 0.126111908574153i
Columns 3 through 4
0.247680627165406 - 0.266531263952234i 0.241196455059011 - 0.228048881621474i
0.108888570419545 + 0.185844471726056i 0.120776872573937 + 0.126111908574153i
-0.299379082744408 - 0.712248715066268i 0.117567427705390 + 0.141276093548378i
0.117567427705390 + 0.141276093548378i -0.295099111426992 - 0.782732763778989i
0.143871692707784 + 0.151690889662265i 0.122038522191192 + 0.146578310214484i
0.197645148819289 + 0.086469414249832i 0.174837759186374 + 0.090370774010972i
0.117567427705390 + 0.141276093548378i 0.098705214275754 + 0.135546322087997i
Columns 5 through 6
0.256961472320009 - 0.276649641863618i 0.133261250722423 - 0.366115587638407i
0.146396214605169 + 0.134241260584520i 0.193042562906943 + 0.068867826288139i
0.143871692707784 + 0.151690889662265i 0.197645148819289 + 0.086469414249832i
0.122038522191192 + 0.146578310214484i 0.174837759186374 + 0.090370774010972i
-0.356756058792799 - 0.700291843335444i 0.205124534993721 + 0.089683840811678i
0.205124534993721 + 0.089683840811678i -0.714466161533014 - 0.228144012364180i
0.122038522191192 + 0.146578310214484i 0.174837759186374 + 0.090370774010972i
Column 7
0.241196455059011 - 0.228048881621474i
0.120776872573937 + 0.126111908574153i
0.117567427705390 + 0.141276093548378i
0.098705214275754 + 0.135546322087997i
0.122038522191192 + 0.146578310214484i
0.174837759186374 + 0.090370774010972i
-0.295099111426992 - 0.782732763778989i
val(:,:,2) =
Columns 1 through 2
0.052239491720805 + 0.249633268228536i 0.195982542613993 - 0.285327107599289i
0.195982542613993 - 0.285327107599289i -0.383645212007223 - 0.694018294127828i
0.226832798396488 - 0.283458929482082i 0.103212760091345 + 0.186006324597861i
0.223124121055869 - 0.242109450058533i 0.117847888445974 + 0.125267691937897i
0.235320452001018 - 0.292970425208942i 0.143895062371979 + 0.133515185072777i
0.097517986050057 - 0.379171534621326i 0.193937840715969 + 0.064921139918509i
0.223124121055869 - 0.242109450058533i 0.117847888445974 + 0.125267691937897i
Columns 3 through 4
0.226832798396488 - 0.283458929482082i 0.223124121055869 - 0.242109450058533i
0.103212760091345 + 0.186006324597861i 0.117847888445974 + 0.125267691937897i
-0.273218269926504 - 0.726686899215550i 0.113684845449206 + 0.140048968515918i
0.113684845449206 + 0.140048968515918i -0.266289719601163 - 0.798052608649804i
0.140299386855742 + 0.150669037029621i 0.117407071257526 + 0.145173766877842i
0.197893421104694 + 0.082748128317030i 0.173803495797223 + 0.087365623688076i
0.113684845449206 + 0.140048968515918i 0.094000225223949 + 0.133884213486492i
Columns 5 through 6
0.235320452001018 - 0.292970425208942i 0.097517986050057 - 0.379171534621327i
0.143895062371979 + 0.133515185072777i 0.193937840715969 + 0.064921139918509i
0.140299386855742 + 0.150669037029622i 0.197893421104694 + 0.082748128317030i
0.117407071257526 + 0.145173766877842i 0.173803495797223 + 0.087365623688076i
-0.334955082203503 - 0.716346161589314i 0.204676248952931 + 0.086022470239099i
0.204676248952931 + 0.086022470239099i -0.718145330586223 - 0.217180534057895i
0.117407071257526 + 0.145173766877842i 0.173803495797223 + 0.087365623688076i
Column 7
0.223124121055869 - 0.242109450058533i
0.117847888445974 + 0.125267691937897i
0.113684845449206 + 0.140048968515918i
0.094000225223949 + 0.133884213486492i
0.117407071257526 + 0.145173766877842i
0.173803495797223 + 0.087365623688076i
-0.266289719601163 - 0.798052608649804i
val(:,:,3) =
Columns 1 through 2
0.079748714189379 + 0.244310557041399i 0.172742441761397 - 0.300777698924675i
0.172742441761397 - 0.300777698924675i -0.365883674100770 - 0.704872034904981i
0.203948886948174 - 0.299593095685179i 0.094770065328589 + 0.187992704028740i
0.204646883671649 - 0.254457824219378i 0.115305783941255 + 0.124490369660443i
0.213134079230373 - 0.307331930776544i 0.141844683165066 + 0.132860467111499i
0.060559084735237 - 0.388763776932741i 0.195270476090546 + 0.060594185631350i
0.204646883671649 - 0.254457824219378i 0.115305783941255 + 0.124490369660443i
Columns 3 through 4
0.203948886948174 - 0.299593095685180i 0.204646883671649 - 0.254457824219378i
0.094770065328589 + 0.187992704028740i 0.115305783941255 + 0.124490369660442i
-0.250519862478070 - 0.737982259533042i 0.110206554564425 + 0.138891920308103i
0.110206554564425 + 0.138891920308103i -0.238209499370146 - 0.811406344407611i
0.137188740426846 + 0.149727942080303i 0.112482714935798 + 0.144308966663099i
0.198611225165208 + 0.078696685275170i 0.172739566022177 + 0.084915552944733i
0.110206554564425 + 0.138891920308103i 0.089025678833481 + 0.132642828755022i
Columns 5 through 6
0.213134079230373 - 0.307331930776544i 0.060559084735237 - 0.388763776932741i
0.141844683165065 + 0.132860467111499i 0.195270476090546 + 0.060594185631350i
0.137188740426846 + 0.149727942080302i 0.198611225165208 + 0.078696685275170i
0.112482714935798 + 0.144308966663098i 0.172739566022178 + 0.084915552944733i
-0.313975972102224 - 0.730429311303543i 0.204246429867100 + 0.082982529154565i
0.204246429867099 + 0.082982529154564i -0.722121125211680 - 0.202345937951177i
0.112482714935798 + 0.144308966663098i 0.172739566022178 + 0.084915552944733i
Column 7
0.204646883671649 - 0.254457824219378i
0.115305783941255 + 0.124490369660442i
0.110206554564425 + 0.138891920308103i
0.089025678833481 + 0.132642828755022i
0.112482714935798 + 0.144308966663099i
0.172739566022177 + 0.084915552944733i
-0.238209499370146 - 0.811406344407611i
Column 7
0.204646883671649 - 0.254457824219378i
0.115305783941255 + 0.124490369660442i
0.110206554564425 + 0.138891920308103i
0.089025678833481 + 0.132642828755022i
0.112482714935798 + 0.144308966663099i
0.172739566022177 + 0.084915552944733i
-0.238209499370146 - 0.811406344407611i
I want to store this data in text file. Hope now my question make sense.
Did you try functions such as writematrix of fprintf?
No I havent tried that but will do, thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by