Loading a cell in Matlab - matlab

I have saved a Matlab file in the computer memory called data.mat. The file is structured as a cell with dimension 13x1. To save the file, I have used the command save('data.mat', 'data').
Now, I want to load the file and transform it.
This is what I am doing at the moment
%Load data
load data
%Transform data
for n=1:13
data{n}(:,1)=rand(10,1); %replace first column
end
However, I get as a warning message on the Matlab script:
"The variable data appears to change size at every loop iteration. Consider preallocating for speed".
In short, the script does not recognise that I have loaded a cell.
How can I fix this? It is not an error message and Matlab does OK when running the code. I just want to remove the warning thing.

You're getting this warning because the Editor has no way of knowing what format your variable data will have until it is loaded from the MAT file. Since data isn't defined, it likely assumes you are making it from scratch and issues the warning. If you're certain you aren't going to be growing your cell array (i.e. you will only ever modify existing cells), you can just suppress the warning by adding this as the first comment on that line:
%#ok<SAGROW>
Or by right clicking on the highlighted word data and selecting the options "Suppress.. On This Line", which will add the comment for you.
More information on this can be found in the documentation.

Related

Matlab s2p file

I'm trying to build a s2p file in simulink using the block "To File" but gives me an odd file with random characters like
fs%($&%%(&
Which looks like when I try to open a jpeg file with block note.
I'm trying a simple RF layout with a single resistive divider and the input and output ports like in figure
anyone knows what is happening here?
The To File block writes a .mat (binary) data file. Opening one as a text file is always going to show odd random characters
To see what's in the file you need to use load to load the saved signal(s) into the MATLAB Workspace.

How to create blank .mat file from terminal?

Is there any way to create an empty .mat file from a terminal session? Basically, what I am doing is brain graph analysis. The software I am using, if an entire brain is scrubbed (ie, if the displacement of the brain is greater than a certain threshold) the output file will be left out or will be very small. When analyzing, however, I need to be able to eliminate both subjects from the analysis if the entire brain is scrubbed/too much of the brain is scrubbed. To accomplish this, the easiest way would be to simply check the dimensions of the output file within matlab, and if they are below the arbitrary threshold I decide then both subjects will just be skipped over for analysis. The issue is, I can easily check if a file contains too few remaining frames, however, if the resulting file contains no frames, it will entirely just not exist. As the outputs are all sorted, the only thing I need to do is check consecutive files' dimensions, and if one of the files does not contain enough values, then I can simply skip over it entirely. Simply touching a blank file obviously will not work, since it will not contain any encoding. I hope this is a good explanation for my motivation to do this, and if any of you know of any suggestions, please let me know.
A simple solution would be to create an empty file from Matlab and duplicate the file when needed from the console.
Just open Matlab, set to the destination folder and type this:
clear all
save empty.mat
Then, when needed, copy the file from the console. :)
Saving the contents of an empty struct creates an empty .mat file:
emptyStruct = struct;
save('myFile.mat','-struct','emptyStruct');

Saving displayed values on the command window - MATLAB

I had a GUI to collect all data and to save it when I clicked a button. It displays a graph with all the data it collects. Unfortunately after collecting data for over 2 hours, it stopped. The data was 'precious'.
I don't know of any way to recover the data, but all of it was displayed in the command window. Is there a way I can retrieve all the data from the command window? Does MATLAB have a cache with all the values which I can access?
As long as matlab itself, (and the console in particular) is still responsive, you can just copy and paste everything out of the buffer into any text editor. Then, you can do a few minor edits, to get it into a text matlab input file, and you're good to go. Or if you've formatted your output correctly, you could put it into a text file, and read it in using readtable.
The key issue here, is how much memory do you have in your console window? I'm sure that can be tweaked up large, but did you bother to do that before you started running your GUI.
Note: In the future, always dump data to a log file as soon as it's collected! I've learned this lesson many times, the hard way.

MATLAB GUIDE GUI errors following function name change

I have renamed a .fig and associated .m file generated by MATLAB's GUIDE. Having done this, I receive a long list of error messages including the following (just a sample of them shown below):
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit34_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit33_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit32_CreateFcn',hObject,eventdata,guidata(hObject))
These errors all seem to relate to individual items on my GUI, such as buttons, text boxes etc.
The function and .fig file used to be called my_gui.m and my_gui.fig. However, I have sinced changed the name to my_new_gui.m and my_new_gui.fig (I've simplified the actual names for the purposes of this question).
So, the obvious solution is to go into the .m file and replace all instances of my_gui with my_new_gui. However, I've done this, and the same error message appears. I have no idea where MATLAB is the reading text my_gui from, since it doesn't exist in any of my code... Any help would be appreciated!
Edit I've discovered that these old references are written in the callbacks for each item on the GUI, which I can change by opening the Property Inspector for each individual item. However, I have a lot of items. If anyone can offer a solution to quickly edit these using a text editor, rather than clicking each individual one, I'd appreciate it!
Renaming MATLAB GUI should be done using Save As... rather than manually changing the file names. Change the file names back to the original names and change the name using Save As... option in GUIDE. This should automatically rename everything.

Is it possible to add data into a matlab cell without loading the cell into the memory?

I have a large .mat file. I call it D.mat. It has a cell inside it (D.X is a cell array). I need to add more data to the end of its only column and I cannot load it into the memory.
I read about this function: memmapfile and Mapping Multiple Data Types and Arrays section, but it seems that I should know the structure of the data and in fact the D.X elements has no definite structure.
I thought maybe there is a function like fgetl for these kind of situations (I mean read the file to its last index (or row) and then write to it).
Is it possible?
Any help is appreciated.
If you have access to version R2011b or later, you can use the matfile command to load and save parts of variables in a .mat file, without loading the data into memory. See doc matfile for more information and some examples.
fgetl is not what you need: it's for reading lines from a text, not .mat file. memmapfile also requires that the data file be laid out in a regular structure, which is not the case for a .mat file.
Before R2011b there is no way I know of doing what you need.