Save and load fft to .mat file - matlab

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);

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');

calling function which reads data from .dat file

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.

how to present .mat data in matlab

I imported my data from excel to matlab and saved it as 't.mat'.I used function:
load('t.mat')
open('t.mat')
ans =
ndata: [62x8 double]
text: {63x9 cell}
alldata: {63x9 cell}
but it did not show the data.I want to see the column 5 of the data an plot it,so i wrote :
x=t.mat(:,5)
plot(x)
the error was :
??? Attempt to reference field of non-structure array.
please help me to present the data and plot it.
thanks
You can load your data into a structure and easily access them:
DataStruct = load('t.mat');
A = DataStruct.alldata; % Assign a variable to alldata.
plot(A{:,5});
Following on #Darthbit's comment, once you load the .mat file ,variables are available in the workspace, so you could use something like this:
load('t.mat');
plot(alldata{:,5})
't.mat' is just the name of the file where your data is stored. When you load a file, it loads the content into your workspace with the saved variable names, here ndata, text and alldata.
You then need to call the variables themselves to access the data:
load('t.mat')
x = ndata(:,5);
plot(x)
I haven't used Matlab in a while but I'm pretty sure you can't just say:
x=t.mat(:,5)
I think you have to store the t.mat into something. Here's a page that might be helpful:
http://www.mathworks.com/help/matlab/ref/load.html#btm3ohm-1

Matlab save sequence of mat files from convertTDMS stored in cell array to sequence of mat files

I have data stored in the .tdms format, gathering the data of many sensors, measured every second, every day. A new tdms file is created every day, and stored in a folder per month. Using the convertTDMS function, I have converted these tdms files to mat files.
As there are some errors in some of the measurements(e.g. negative values which can not physically occur), I have performed some corrections by loading one mat file at a time, do the calculations and then save the data into the original .mat file.
However, when I try to do what I described above in a loop (so: load .mat in folder, do calculations on one mat file (or channel therein), save mat file, repeat until all files in the folder have been done), I end up running into trouble with the limitations of the save function: so far I save all variables (or am unable to save) in the workspace when using the code below.
for k = 1:nFiles
w{k,1} = load(wMAT{k,1});
len = length(w{k,1}.(x).(y).(z));
pos = find(w{k,1}.(x).(y).(z)(1,len).(y)<0); %Wind speed must be >0 m/s
for n = 1:length(pos)
w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)) = mean([w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)+1),...
w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)-1)],2);
end
save( name{k,1});
%save(wMAT{k,1},w{k,1}.(x),w{k,1}.ConvertVer,w{k,1}.ChanNames);
end
A bit of background information: the file names are stored in a cell array wMAT of length nFiles in the folder. Each cell in the cell array wMAT stores the fullfile path to the mat files.
The data of the files is loaded and saved into the cell array w, also of length nFiles.
Each cell in "w" has all the data stored from the tdms to mat conversion, in the format described in the convertTDMS description.
This means: to get at the actual data, I need to go from the
cell in the cell array w{k,1} (my addition)
to the struct array "ConvertedData" (Structure of all of the data objects - part of convertTDMS)
to the struct array below called "Data" (convertTDMS)
to the struct array below called "MeasuredData" (convertTDMS) -> at this level, I can access the channels which store the data.
to finally access/manipulate the values stored, I have to select a channel, e.g. (1,len), and then go via the struct array to the actual values (="Data"). (convertTDMS)
In Matlab format, this looks like "w{1, 1}.ConvertedData.Data.MeasuredData(1, len).Data(1:end)" or "w{1, 1}.ConvertedData.Data.MeasuredData(1, len).Data".
To make typing easier, I took
x = 'ConvertedData';
y = 'Data';
z = 'MeasuredData';
allowing me to write instead:
w{k,1}.(x).(y).(z)(1,len).(y)
using the dot notation.
My goal/question: I want to load the values stored in a .mat file from the original .tdms files in a loop to a cell array (or if I can do better than a cell array: please tell me), do the necessary calculations, and then save each 'corrected' .mat file using the original name.
So far, I have gotten a multitude of errors from trying a variety of solutions, going from "getfieldnames", trying to pass the name of the (dynamically changing) variable(s), etc.
Similar questions which have helped me get in the right direction include Saving matlab files with a name having a variable input, Dynamically Assign Variables in Matlab and http://www.mathworks.com/matlabcentral/answers/4042-load-files-containing-part-of-a-string-in-the-file-name-and-the-load-that-file , yet the result is that I am still no closer than doing manual labour in this case.
Any help would be appreciated.
If I understand your ultimate goal correctly, I think you're pretty much there. I think you're trying to process your .mat files and that the loading of all of the files into a cell array is not a requirement, but just part of your solution? Assuming this is the case, you could just load the data from one file, process it, save it and then repeat. This way you only ever have one file loaded at a time and shouldn't hit any limits.
Edit
You could certainly make a function out of your code and then call that in a loop, passing in the file name to modify. Personally I'd probably do that as I think it's neater solution. If you don't want to do that though, you could just replace w{k,1} with w then each time you load a file w would be overwritten. If you wanted to explicitly clear variables you can use the clear command with a space separated list of variables e.g. clear w len pos, but I don't think that this is necessary.

Matlab: How to remove the error of non-existent field

I am getting an error when running matlab code. Here I am trying to use one of the outputs of previous code as input to my new code.
??? Reference to non-existent field 'y1'.
Can anyone help me?
A good practice might be to check if the field exists before accessing it:
if isfield( s, 'y1' )
% s.y1 exists - you may access it
s.y1
else
% s.y1 does not exist - what are you going to do about it?
end
To take Edric's comment into account, another possible way is
try
% access y1
s.y1
catch em
% verify that the error indeed stems from non-existant field
if strcmp(em.identifier, 'MATLAB:nonExistentField')
fprintf(1, 'field y1 does not exist...\n');
else
throw( em ); % different error - handle by caller?
end
end
Have you used the command load to load data from file(s)?
if yes, this function overwrite your current variables, therefore, they become non-existent, so when you call, it instead of using:
load ('filename');
use:
f=load ('filename');
now, to refer to any variable inside the loaded file use f.varname, for
example if there is a network called net saved within the loaded data you may use it like:
a = f.net(fv);
I would first explain my situation and then give the solution.
I first save a variable op, it is a struct , its name is coef.mat;
I load this variable using coef = load( file_path, '-mat');
In a new function, I pass variable coef to it as a parameter, at here, the error Reference to non-existent field pops out.
My solution:
Just replace coef with coef.op, then pass it to the function, it will work.
So, I think the reason behind is that: the struct was saved as a variable, when you use load and want to acess the origin variable, you need point it out directly using dot(.) operation, you can directly open the variable in Matlab workspace and find out what it wraps inside the variable.
In your case, if your the outputs of previous code is a struct(It's my guess, but you haven't pointed out) and you saved it as MyStruct, you load it as MyInput = load(MyStruct), then when use it as function's parameter, it should be MyInput.y1.
Hops it would work!
At first load it on command window and observe the workspace window. You can see the structure name. It will work by accessing structure name. Example:
lm=load('data.mat');
disp(lm.SAMPLE.X);
Here SAMPLE is the structure name and X is a member of the structure