Plotting column of a matrix from a string.

1 view (last 30 days)
Rich
Rich on 26 Mar 2015
Commented: Rich on 26 Mar 2015
I have a variable which contains data loaded from a matfile...
loadedContent=load('C:\Users\ringo\AppData\Local\Temp\temp3388979983628797436tmp\mv_20.mat');
in that matfile is a 360000x5 double called dat. I need to plot a column from dat using a char variable. I can plot all of the columns by creating a char variable
plotsall = 'dat';
plot(loadedContent.(num2str(plotsall)));
but if i try
plotcolumn = 'dat(:,1)';
plot(loadedContent.(num2str(plotcolumn)));
i get an error
Reference to non-existent field 'dat(:,1)'.
The only way round it I can think is to actually load the data into the workspace and plot using
plot(eval(plotcolumn));
this works, but using the eval function is never good... i just can't think of a way round it. Any ideas?
If you want/need anymore info as to why i'm doing this, a short version is making gui-->user locates file--->contents of file loaded into a list box--->user selects which data to plot--->if the data has more than one column a popup asks them which column to plot--->data gets plotted.
If you need anymore info, please let me know. Cheers

Accepted Answer

Adam
Adam on 26 Mar 2015
plot( loadedContent.( plotsall )(:,1) )
seems to work. I wasn't aware it could be done because usually Matlab doesn't support chained indexing, but since loadedContent.( plotsall ) evaluates to a variable you can index it as any other variable.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!