How to plot data of only specified rows of matrix

32 vues (au cours des 30 derniers jours)
Anthony Koning
Anthony Koning le 11 Avr 2023
Commenté : Star Strider le 11 Avr 2023
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,:))

Réponses (2)

VBBV
VBBV le 11 Avr 2023
Modifié(e) : VBBV le 11 Avr 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 commentaires
Anthony Koning
Anthony Koning le 11 Avr 2023
I unfortunately cannot currently upload the data.mat file. Additionally, loading the code you've adjusted just results in an error message of "Unrecognized field name "Var1"."
VBBV
VBBV le 11 Avr 2023
Modifié(e) : VBBV le 11 Avr 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,:))

Connectez-vous pour commenter.


Star Strider
Star Strider le 11 Avr 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 commentaires
Anthony Koning
Anthony Koning le 11 Avr 2023
For line 5, using this code results in an error with the .y portion of the code, claiming "Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses." What would be causing this?
Star Strider
Star Strider le 11 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by