Matlab File Loading

The Rocketry Forum

Help Support The Rocketry Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

edwardw

Well-Known Member
Joined
Jan 9, 2004
Messages
2,126
Reaction score
0
Does anyone here work with Matlab? What I'm trying to do is a load a file and then manipulate that data. So far I'm good in loading the file. I input the file name and it loads it just fine. For example, say I load a file called 'data.dat' into Matlab. Now in my workspace there is a matrix that has the name data I want to manipulate the data in there, but since the file changes all the time I just can't set it constant. Usually my command would look like this T=data:),1)
Basically I'm taking the first column and putting it in its own matrix. I can't do that now because sometimes the file might change to zoo.dat or hungryhippo.dat

So, I tried inputing what the matrix would be as a string so then I'd have a variable, say matrix name, mn=hungryhippo

When I tried the same command T=mn:),1) it hates me. Anyone have suggestions?

Edward
 
are you trying to write an "m" file that will programatically read in file names?

you could use the "str" and "eval(str)"

ex.

mn= 'hungryhippo.dat';
str=['load ' mn ';'];
eval(str)

there is also a way to set up a function and use one of the input arguments in a string, but I forgot and all my macros are at work
 
I can load in the file names just file. They then turn to matricies. I'm having trouble manipulating those matricies. When I load data.dat I get matrix 'data'.

I want to work on that one called 'data'. It may be called many other names. I want to find a way to be able to substitute whatever name the file is in functions such as T='filename':),1)

:)

why (pi^2)

Edward
 
OK, I had to review some basic matlab @ work today, and since the forum was down, I couldn't get this to you...

you were close, you need to make the filename a string like so:

mn='data.dat';

then you can set the variable name (the matrix) to the values in the filename

T=load(mn);

If you have multiple columns of data, the matrix be be "x" columns by however many number of values you have. You can access the different columns like this:

T:),1); % first column
T:),2); % second column
T:),3); % third column
T:),4); % forth column

If you want to set a variable to equal the values of that first column use something like this:

col1=T:),1);

then if you set the string variable to your new filename, and re-run the "T=load(mn);" it will read in the new values.

mn='hungryhippo.dat';
 
BUMP!!

Is there a good way to do a GOTO type statement? I've got a file that after I do all my calculations and look at the data I want to the option to re-do them with the same parameters, without having to input all my starting conditions (which there are quite a few)?

Thanks a ton!

Edward
 
goto type statements:

N=10;
data=zeros(N,1);
parameter=0:1/(N-1):1;

while(Boolean)
statements;
end


for i=1:N
statements;
stuff(i)=function(data(i,1),parameter(i));
end

////////
Here is an example of code that uses a while loop to iterate for a solution:
///////////
function M = invPM(nu,k)
% Inverse Prandtl-Meyer function (using Newton Rhapson)
dM = .1; % Leave at about .1
M=1;
res = 1;%set initial resolution
oldres =1.1;
i=0;
while res > .001
M2 = M + dM;
funv1 = (-nu+(sqrt((k+1)/(k-1))*atan((sqrt((k-1)*(M^2-1)/(k+1))))-atan(sqrt(M^2-1))));
funv2 = (-nu+(sqrt((k+1)/(k-1))*atan((sqrt((k-1)*(M2^2-1)/(k+1))))-atan(sqrt(M2^2-1))));
dv_dm = (funv2-funv1)/dM; %inverse slope for iteration
M = M - funv1/dv_dm; %redefine M from the step size and the slope
res = abs(funv1); %if not precise enough, re-iterate
i=i+1;
if i>1000
'invPM function did not converge'
M=0;
break
end
end



sorry, the spacing doesn't seem to work on this forum.
 
Back
Top