looping thru multiple line data in UIAxes plot and writing all X,Y data to an array

Hello, i have a set of line plots on a UIAxes on a UIFigure using Appdesigner.
Note each plot has differing X coordinates
I want a way to be able to save all these plots so that I can revisit later and add further plots to it.
The approach I was going to take was to access the line objects and loop through assigning each X,Y datat set to a data array that I could save out and then recal at a later date.
ax=app.UIAxes;
L=ax.XLim;
L2=L(2); % Get max number on x-axis
h1 = findall(ax, 'type', 'line') % Get just line objects for now
n=numel(h1); % Number of line plots (will do text objectes later)
H=nan(L2,2*n) % Create an array of Nans to hold all data (row,cols)
% Each line requires its X data to be saved also
Now loop thru' all line items and splat the X,Y data to the array
for(i=1:n)
t = h1(i)
H(:,(2*i)-1)=(t.XData)';
H(:,2*i)=(t.YData)';
end
H
But Im getting the following error
Unable to perform assignment because the size of the left side is 120-by-1 and the size of the right side is 100-by-1.
Error in GenericDataAnalysis/ReadHeadersButtonPushed (line 533)
H(:,(2*i)-1)=(t.XData)';
I suspect its to do with non-equal length of data sets. Perhaps I need some sort of padding for the missing data?
This is my current output:
L2 =
120
h1 =
7×1 Line array:
Line (20s Delay)
Line (15s Delay)
Line (9s Delay)
Line (5s Delay)
Line (2s Delay)
Line (FC Top)
Line (FC Btm)
The second part of my question, is whats the best approach to retain the colours and text objects?
Remember, i don't just want the graphic saved, I want to be able to add data to it later from within my GUI.
Thanks
Jason

Answers (1)

I would suggest a struct array. That way you can store all relevant properties and you can fairly easy recreate the line objects.

3 Comments

Hi Rik, that is an awesome idea - thanks.
After trying the following: (I see its possible to assign "objects" to a struct field so I've assumed I dont need to loop thru each line plot object, I can just save them "collectively" and just loop thru them once I load them back to give me my plots - is this true?
ax=app.UIAxes;
ax.Children;
h1 = findall(ax, 'type', 'line') % Line objects
nlines=numel(h1);
h2 = findall(ax, 'type', 'text') % Text objects
ntext=numel(h1);
datastruct.nlines=nlines;
datastruct.ntext=ntext;
datastruct.Lines=h1; % Can assign objects, so assign the h1
datastruct.Text=h2;
leg=ax.Legend
% Save as a Mat file
try
[file,folder]=uiputfile({'*.mat','Matlab Files'},'Save Data',app.startfolder);
catch
[file,folder]=uiputfile({'*.mat','Matlab Files'},'Save Data','C:\');
end
app.startfolder=folder;
savepath=fullfile(folder,file)
save(savepath,"-struct","datastruct")
But its failing to save correctly.
Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects.
When I try to load it back:
[file,path,idx]=uigetfile({'*.mat'},'Load Mat (Matlab) File',app.startfolder);
path1=fullfile(path,file)
variableInfo = who('-file', path1)
s=load(path1);
path1 =
'C:\MATLAB_FILES\APPDESIGNER\GenericDataAnalysis\Data02.mat'
Error using who
Could not open C:\MATLAB_FILES\APPDESIGNER\GenericDataAnalysis\Data02.mat as a valid MAT-file.
Error in GenericDataAnalysis/LoadPlotMatButtonPushed (line 625)
variableInfo = who('-file', path1)
You're fairly close, but the errors are why I suggested using a struct array instead of an object array. Each object contains information about the parent object as well. that means you can only save and restore your current objectes, which isn't the goal.
By using a struct you can store all the properties you need to reconstruct the objects from scratch.
Don't be afraid of a for loop, they are very fast in Matlab.
Hi, when I tried this, I created a field in the Struct where I assign the complete line objects to the struct.
But then when I tried to save this out in a mat file, it complained it couldnt save the objects.
h1 = findall(ax, 'type', 'line') % Line objects
datastruct.Lines=h1;
thats why I switched to usign "variables" instead

Sign in to comment.

Categories

Products

Release

R2023b

Tags

Asked:

on 10 Jun 2024

Commented:

on 11 Jun 2024

Community Treasure Hunt

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

Start Hunting!