How to plot data of only specified rows of matrix
11 views (last 30 days)
Show older comments
Hi, I'm currently trying to plot a line from rows 1 and 7 of an 8*1000 single matrix from data imported from a .mat file, however, whenever I try to run it, Matlab keeps telling me there's some type of error in my plot arguement. If someone could explain to me what I'm doing wrong, that would be appreciated. Thank you very much. My sript is as follows:
y = load("data.mat");
figure
hold on
plot(y(1,:))
plot(y(7,:))
0 Comments
Answers (2)
VBBV
on 11 Apr 2023
Edited: VBBV
on 11 Apr 2023
The load function imports data to a struct which contains the variables. To access them you need to use a dot operator, Shown here is a variable named Var1 contained in struct
y = load("data.mat");
figure
hold on
plot(y.Var1(1,:))
plot(y.Var1(7,:))
3 Comments
VBBV
on 11 Apr 2023
Edited: VBBV
on 11 Apr 2023
Ok. That's because I don't have your data.mat file and the field variables inside that file may be differently named. To just show how it works, I have assumed it as Var1. To access the variable data and plot you can follow the code which I have shown but replace Var1 with name that's actually present in your data.mat file.
y.X = randi([0 8],8,1000); % random data with field variable X
hold on
plot(y.X(1,:))
plot(y.X(7,:))
Star Strider
on 11 Apr 2023
It is straightforward to create ‘y’ to test the code —
y = randn(8,1000); % Create 'y'
save('data.mat', 'y') % 'save' 'y' To A '.mat' File
data = load("data.mat"); % 'load' To A Structure
y = data.y; % Retrieve 'y' From The 'data' Structure
figure
hold on
plot(y(1,:))
plot(y(7,:))
The original problem was likely that plot cannot plot structures, that ‘y’ originally was in this context, so it is necessary to recover the matrix from the ‘data.mat’ file structure first.
.
2 Comments
Star Strider
on 11 Apr 2023
I don’t have your ‘data.mat’ to work with so I created my own to test my code. I’m assuming that your ‘data.mat’ is similar to the one I created. If it isn’t, and since I have no idea what is in it, I can’t solve that problem.
Please run this from a script or your Command Window and then copy that result and paste it to a Comment here:
LD = load("data.mat")
That will at least tell me what is in the file. I also need to know what variable you want to retrieve from it and what you want to plot. Just now, none of that has been presented.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!