필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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

채택된 답변

KSSV
KSSV 2017년 4월 3일
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

추가 답변 (1개)

Stephen23
Stephen23 2017년 4월 3일
편집: Stephen23 2017년 4월 3일
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:

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by