Effacer les filtres
Effacer les filtres

Reading a file, storing data as variables, but unable to access them?

7 vues (au cours des 30 derniers jours)
Sarah Malik
Sarah Malik le 12 Oct 2017
Modifié(e) : Rena Berman le 31 Oct 2017
Original:
Hello! I'm trying to access the variable 't' from my function, which is designed to read the information from a data file. The file has three columns and two headers, so I skip over the two headers and store the columns in the variables t, y, and s. I need to then compute the Jacobian of the 't' value with respect to the initial equation I was given. My issue is, however, when I run my program, I get 'ans' instead of any variable 't', even though I seem to be able to plot the variables read from the file just fine.
function [t, y, s] = read_datafile(filename)
fid = fopen(filename, 'r');
header = fgetl(fid); % Omit first header.
header2 = fgetl(fid); % Omit second header.
blank_line = fgetl(fid);
t = []; y = []; s = [];
counter = 0;
while (feof(fid) == 0)
counter = counter + 1;
line = fgetl(fid);
data = str2num(line);
t(counter, 1) = data(:, 1);
y(counter, 1) = data(:, 2);
s(counter, 1) = data(:, 3);
end
fclose(fid);
errorbar(t, y, s, 'o'); % Plot the data from the file in an xy-graph with error bars.
xlabel('t(seconds)');
ylabel('Amplitude(V)');
This becomes a problem when I have to move on to the second function, which calculates the Jacobian. It's a user-defined function as opposed to the built-in Jacobian (requirements of the assignment).
function J = jacob(a, t)
A = [1; 1; 1]
dyda1 = A(1)*t; dyda2 = A(2)*-10.*t.*sin(t); dyda3 = A(3)*t.^2;
J = [dyda1, dyda2, dyda3];
These functions are being run on separate m-files. I could compute the Jacobian after running read_datafile('data02.dat') and then entering jacob(A, ans) in the command prompt, but I want to be able to store the value of 'ans' from the previous function as a variable, 't'. How can I do this, so that I can instead write jacob(A, t)?
Thanks in advance for any assistance and insight!
Later edit:
Hey guys, I took a long hard look at my code and figured out how to fix it, thanks to some insightful comments. Thanks for your assistance!
EDIT: I wanted to be really sure that my program worked before posting it, I'll show the corrections I made below.
% Main script:
t = []; y = []; s = [];
% Call the read_datafile function:
[t, y, s] = read_datafile('data02.dat');
% Define the model parameter vector alpha (a):
a = [1; 1; 1];
% Compute the Jacobian by calling the jacob function:
J = jacob(a, t);
%%%%%%%%Read Datafile Function - separate m-file %%%%%%%%
function [t, y, s] = read_datafile(filename)
fid = fopen(filename, 'r');
header = fgetl(fid);
header2 = fgetl(fid);
blank_line = fgetl(fid);
t = []; y = []; s = [];
counter = 0;
while (feof(fid) == 0)
counter = counter + 1;
line = fgetl(fid);
data = str2num(line);
t(counter, 1) = data(:, 1);
y(counter, 1) = data(:, 2);
s(counter, 1) = data(:, 3);
end
fclose(fid);
errorbar(t, y, s, 'o');
xlabel('t(seconds)');
ylabel('Amplitude(V)');
%%%%%%%%Jacobian Function (user-defined) - separate m-file %%%%%%%%
function J = jacob(a, t)
% Manually compute the partial derivatives for the Jacobian:
dyda1 = a(1)*t; dyda2 = a(2)*-10.*t.*sin(t); dyda3 = a(3)*t.^2;
J = [dyda1, dyda2, dyda3];
  7 commentaires
dpb
dpb le 13 Oct 2017
"being really simple and easy to solve--a result of my misunderstanding of how to call functions. I didn't think it would be useful to most people, ..."
But it wasn't so simple before the answer was pointed out, was it? (Else't you wouldn't have needed to post in the first place). Just because end solution is simple doesn't necessarily imply the question isn't worthy of note and removing the question leaves all the responses hanging out of context.
Going forward, make updates/modifications as comments; reserve the Question for the original question with, perhaps Addenda to amplify or clarify questions responders have or to add sample data or the like.
Rena Berman
Rena Berman le 31 Oct 2017
(Answers Dev) Restored edit

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 12 Oct 2017
"...when I run my program, I get 'ans' instead of any variable 't'"
Because you're calling the function without a LHS into which to store the returned variables from the function; ans is the default Matlab variable for a return value when no variable is given. It also is normal Matlab syntax that all except the first variable of multiple return variables from a function are discarded unless there are explicit variables provided on LHS of the calling expression for them to be assigned into.
I'd rewrite the function more like
function data = read_datafile(filename)
data = importdata(filename,);
data=data.data;
errorbar(data(:,1), data(:,2), data(:,3), 'o'); % Plot the data from the file in an xy-graph with error bars.
xlabel('t(seconds)');
ylabel('Amplitude(V)');
and return the data as an array instead of three variables. Alternatively, consider using a table, kinda' the best of both; a single structure but with names for variables within.
  3 commentaires
Cedric
Cedric le 13 Oct 2017
Don't forget to accept the answer if it helped.
Sarah Malik
Sarah Malik le 13 Oct 2017
I accepted it this morning after thoroughly testing my corrections, thanks for reminding me.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by