calling function which reads data from .dat file - matlab

Warning: Direct access of structure fields returned by a function call (e.g.,
call to INPU) is not allowed. See MATLAB 7.10 Release Notes, "Subscripting Into Function Return Values" for details.
In Main at 3
??? Attempt to reference field of non-structure array.
Error in ==> Main at 3
INPU.m;
getting this error...how to call this inpu.m file.
my input file is below.
it reads data from dat file.
%---INPU.m----
%This file reads the input data from the file ‘‘'ex.dat'" and saves them in vector form
n=csvread('ex.dat',1,0,[1,0,1,0]);
constr=csvread('ex.dat',4,0,[4,0,4,(3*n-1)]);
q=csvread('ex.dat',7,0,[7,0,7,(3*(n+1)-1)]);
r=csvread('ex.dat',10,0,[10,0,10,(3*n-1)]);
x=csvread('ex.dat',13,0,[13,0,13,(3*n-1)]);
dx=csvread('ex.dat',16,0,[16,0,16,(3*n-1)]);

To call a function, you use it's name, not the filename (which has a .m file extension). The function name is the portion of the filename without the .m. In your case, since your function is saved in the file INPU.m, you just invoke it with INPU.

Related

How to use saveas() to save a .mat or .txt in specific directory on ubunto

I have tried:
saveas(keys ,keyname);
save(keys ,keyname ,'-mat');
save(keys ,keyname);
where keys is a 3006x4104 matrix of the class double.
I tried to use some casting so I would save it as txt file
for keyname I tried - ./newdronephotos/1880key.mat
and newdronephotos/1880key.mat.
It's not working.
In particular, when I'm trying saveas(keys ,keyname);
it's starting to do some processes and then print the error message:
Error using saveas (line 88)
Simulink object array must be a vector
Actually, I just succeeded to solve my own problem.
The function save() expect to have a string of the name of the desired variable.
For example:
Instead of -
save(keyname ,keys);
I needed to write-
save(keyname ,'keys');

Error when trying to access a field in MATLAB structure

I am trying to run a loop over multiple files, by feeding the file names into a function. I have saved these file names as a structure using:
files=dir('testdata\*.siz');
nrows=size(files,1);
Now my loop is:
for i=1:nrows
filename=files.name{i};
Singapore(filename);
writetable(ans,'file.xls')
end
However, I get the error:
"Field reference for multiple structure elements that is followed by more reference blocks is an error."
I found that the error is in
filename=files.name{1};
but everywhere I've searched tells me to use { } to access fields in a structure. I also have tried other types of brackets in vain.
Additional Information:
'files' is the name of the structure
'name' is the first column field within 'files' containing the file names in inverted commas.
You are referencing the files struct wrong, you need:
files(i).name
The {} is for accessing cell arrays.
You should also use ii (or similar) instead of i as your indexing variable since i is a already Matlab variable (imaginary unit).

Save and load fft to .mat file

I have an FFT and I want to save this to .mat file and then read it from that .mat file. I want to use my loaded data for a division. Here is my code
save ('file_fft','a_fft');
b = load ('file_fft');
c = abs (d_fft) ./ abs (b);
In my command window I see that
b = file_fft [4000 * 1 double].
And then I get error message
'Undefined function 'abs' for input arguments of type 'struct''
I tried after deleting abs. I got this error
'Undefined function 'rdivide' for input arguments of type 'struct''
Does anybody have any idea that why it is not working and how i can solve it?
Check the documentation for the load function:
S = load(___) loads data into S, using any of the input arguments in
the previous syntax group.
If filename is a MAT-file, then S is a structure array.
If filename is an ASCII file, then S is a double-precision array
containing data from the file.
So b is a structure and as suggested in the comment, you need to access the data as abs(b.a_fft).
The problem is that load() already creates the file structure that is inside the .mat file. When you assign file_fft with load, it creates a struct.
From the documentation of load():
S = load(_) loads data into S, using any of the input arguments in
the previous syntax group.
If filename is a MAT-file, then S is a structure array.
Edit:
I got sniped :-P
Here's another working example of code:
save ('file_fft','a_fft');
load ('file_fft');
c = abs (d_fft) ./ abs (file_fft);

Matlab not recognizing string variable

I have a variable that is holding a string (the string contains a path to a .mat file). However whenever I call load the variable I get an error saying "Error using load Unable to read file"
Here is my code where I call load:
fName = strcat(fName,'_features.mat');
display(fName);
load(fName);
For those curious fName = '/Users/MATLAB/10360453085_p2_features.mat'
Why am getting an error on load even though when I copy the value of fName into load it works perfectly fine, but using load(fName) gives me an error?
Most likely, fname is initialized somewhere as a cell array. strcat will therefore return a cell array, so that disp will display it as 'name' rather than name.
load(fName{1})
or
load(char(fName))
will work in this case.

Matlab radar/spider plots

I need to make a radar plot, I googled it and got this: http://www.mathworks.co.uk/matlabcentral/fileexchange/33134-radar-plot/content/radarPlot.m
but it gives an error I'm not sure how to solve.
The error is Error: File: radarplottest.m Line: 17 Column: 1
Function definitions are not permitted in this context.
Any tips?
Did you copy the function and paste it into a script file you were working on?
If you did something like add your own code at the top of the file (say, to load data), that will give you the error you're seeing. You should have one file, "radarPlot.m", which contains this function, and then "radarplottest.m" could be something like a script containing data loading/pre-processing and then calling the radarPlot function on the appropriate data.
All you need in radarplottest.m is something like:
Data = % define some random test data or load some existing data
radarPlot(Data); % requires radarPlot.m to be on your path so Matlab can find it