MATLAB Answers

Can't seem to understand what my mistake is (Loading .mat files)

16 views (last 30 days)
Japnit Sethi
Japnit Sethi on 1 Oct 2020 at 18:17
Commented: Japnit Sethi on 1 Oct 2020 at 19:00
So I had saved .mat files from my 2 Matlab codes and now loading into a new Matlab File for a comparison plot!
But I am getting an error that it is an "unrecognized function or variable", when I can clearly see the variable in my workspace
All the files ae attached!!
clear all;
close all;
% Comparison Plot
filenames = {'q_Theta_Y.mat','q_dot_Theta_Y.mat','q_Y_Theta.mat', 'q_dot_Y_Theta.mat',...
'q_ref.mat', 'q_ref_dot.mat', 'Time.mat',...
'E_pos_Theta_Y.mat','E_vel_Theta_Y.mat','E_pos_Y_Theta.mat', 'E_vel_Y_Theta.mat'};
for kk = 1:numel(filenames)
%%%%%%%%%%%%%%%%%%%%%%%% Plot results %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot Comparison b/w q_ref1 and q1 (Position of Link 1)
grid on
grid minor
hold on
plot(Time, q_Y_Theta(:,1), 'r-', Time,'LineWidth',2);
axis tight
title('Link 1: actual and reference trajectory', 'fontsize', 20);
xlabel('$$t \, {\rm [s]}$$', 'interpreter','latex','fontsize', 20);
ylabel('$$Position (m)$$', 'interpreter','latex', 'fontsize', 20);
% xlim([0 t_net(length(t_net))]);
% ylim([-1.2 1.2]);
legend('$$q_1$$($$\Theta \cdot Y$$)', '$$q_{1,{\rm ref}}$$', '$$q_1$$($$Y \cdot \Theta$$)',...
hold off

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 1 Oct 2020 at 18:35
@Japnit Sethi : please show us the complete error message. This means all of the red text.
Why are you saving one variable per file? Your code would probably be simpler if you saved all of those variables in one file (tip: use the -struct option when saving, and always load into an output variable. Note that load also lets you select which variables to import from the file).

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 1 Oct 2020 at 18:39
Edited: Stephen Cobeldick on 1 Oct 2020 at 18:48
The bug is very simple: your code refers to
but the variable in the mat file is named
You need to use the correct case (variable names are case-sensitive).
Much better would be to replace all of those separate files with one mat file and load into an output variable (which is a scalar structure), e.g.:
F = absolute or relative path of the mat file.
S = load(F);
Note that you can select which variables you want to import, e.g.:
S = load(F,'Time','q_Theta_Y');

  1 Comment

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by