How to change variables with same name into new ones instead of structure array

2 views (last 30 days)
Dear Community member, Im new to MATLAB and in need of your assistant. Im running different functions and variables in my script. I have 5 different models from which I get X and Y data. The X data is the same in all models, only Y is different. My goal is to plot these x & y data from the 5 models into 1 plot but I have some trouble. From the script the variables x and y are saved using save('model1x.mat','x'). In a new file i use the load function to load these new .mat files but even though the .mat have different names they keep getting loaded as x and overwrites all the time. when i try to assign them a new name when they get loaded (what i mean is: model1x=load('C:\Users\data\model1\x.mat'), but only as a structure array which I apparantly cannot use for the plot command.
So short story: I have X and Y data saved as a .mat file from 5 different models: model1x.mat model1y.mat model2x.mat model2y.mat I wan't to plot them I was thinking: plot(model1x'o',model1y,'r-',model1x,'o-',model2y,'g-',model1x,'o')
Im really, like really really new to MATLAB, so a step by step guide would be much appreciated. And if you happen to know a good way on forcing the same color in my plot command, so all model1x are circles with the same color please do tell. Thank you for taking your time reading this. Kind Regards Saad

Accepted Answer

KSSV
KSSV on 3 Apr 2017
You load the first file...use plot and plot the data you want. Then use hold on and load the other files and plot one after another...
figure
hold on
% load the first file
% plot
% load the second file
% plot

More Answers (1)

Stephen23
Stephen23 on 3 Apr 2017
Edited: Stephen23 on 3 Apr 2017
Loading that data into a structure is the best way to do this. It might seem complicated, but even as a beginner there is no reason why you should not learn how to do things the best way, right from the start:
Once you have your data in structures, then accessing it is easy:
Sind = load('C:\Users\data\model1\x.mat');
S(1) = load('C:\Users\data\model1\y.mat');
S(2) = load('C:\Users\data\model2\y.mat');
S(3) = load('C:\Users\data\model3\y.mat');
%
plot(Sind.x,S(1).y,'o', Sind.x,S(2).y,'x', Sind.x,S(3).y,.'*')
Note that using indexing like this makes your code fast and efficient, and that you could easily put this code into a loop. Whatever you do, do NOT try to dyanimically name the variables:

Community Treasure Hunt

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

Start Hunting!