plot - Matlab Function fails due to : 'Error using Eval', works fine when used in Command Window -


when plotting data .mat file, if enter lines script one-by-one, works fine... when try running script, fails.

function test (filename)       varname = load (filename)  %or load filename                                                         matobj = matfile(filename);    varlist = (matobj);   %or varlist = fieldnames (varname)      field1 = eval ( varlist {1} )    field2 = eval ( varlist {2} )    x1 = field1.x_values.start_value:field1.x_values.increment:field1.x_values.increment*field1.x_values.number_of_values;    x2 = field2.x_values.start_value:field2.x_values.increment:field2.x_values.increment*field2.x_values.number_of_values;    figure    hold     %support yyaxis left/right not avaiable, use plotyy    plotyy (x1, field1.y_values.values, x2, field2.y_values.values)  end 

when invoke script (test ('1.mat')), matlab shows error on field1 = line :

  error using eval   undefined function or variable 'signal'.   

the 'signal' 1 of data set names in 1.mat file.

interestingly, when run each line in same order command window, don't error , plot displays. verified current path has script , 1.mat file, can't figure out why complains eval when run script.

the issue because matobj *.mat file contains variable named signal. never load file in function (using load) instead assign matfile object matobj. read variable do not use eval (ever), rather want use dynamic fields referencing matfile object.

field1 = matobj.(varlist{1}); field2 = matobj.(varlist{2}); 

in general though, should know name of variables you're trying load file rather using first 2 variables find who. if that's case, use them directly.

field1 = matobj.signal; 

the reason code worked in command window because @ point loaded .mat file command window workspace using load have loaded it's contents (including signal) workspace.

load('filename.mat') 

also bit of nit-pick. don't have script have function (you have function definition @ top). has huge ramifications diagnosing problem. cannot test function copy/pasting stuff command window due different scope of function.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -