save part of matlab cell array, proper refence - matlab

I found the following piece of MATLAB code previously posted by someone here:
x = cell(3,4);
save x;
matObj = matfile('x.mat','writable',true);
matObj.x(3,4) = {eye(10)};
It creates a .mat file with a 3x4 cell array in it, and the content of the cell positioned at (3,4) is a 10x10 identity matrix.
What would be the right syntax to read the .mat file and change the value of the element positioned at (2,3) in the identity matrix to say -5?
If it were possible to use the curly braces I would do it as mat.Obj.x{3,4}(2,3)=-5, but MATLAB says this type of referencing for cell arrays is not supported.
Thanks in advance.

From what I know, there is no way in matlab to reference matlab.io.MatFile objects like you want. The easier way would be just to create a temporary variable to do it.
Thus instead trying to do this:
matObj.x(3,4) = {eye(10)};
matObj.x{3,4}(2,3)=-5; % this will lead to error, as you noticed
do like this:
tmpVar = eye(10);
tmpVar(2,3) = -5;
matObj.x(3,4) = {tmpVar};

Related

convert struct type to matrix octave

I have a myfile.mat that contains 3000x35 size data. When I load it into a variable as :
a = load('myfile.mat')
It gives struct of size 1x1 . How can I get exact form as matrix. This is required because I need to change certain column values.
(This answer is valid for MATLAB, I am not sure it works exactly the same way in Octave)
You have several options:
Option 1:
If the .mat file only contains one variable, you can do:
a = struct2array(load('myfile.mat')); % MATLAB syntax
a = [struct2cell(load('myfile.mat')){:}]; % Octave syntax
Option 2:
You need to know the name of the variable at the time of saving, which is now a name of a field in this struct. Then you would access it using:
a = load('myfile.mat'); a = a.data;
Option 3 (unrecommended!):
Just remove the a = part of the expression,
load('myfile.mat');
Then the variables inside this file will "magically spawn" in your workspace. This is not recommended because it makes it difficult (impossible?) to see when certain variables are created.
I found the solution myself. I did like this
a = load('myfile.mat')
a = a.data
So that now I can access values of a as matrix with indexing, a(1,2) like this.
It is in octave. I hope it is similar in matlab.

How to write multiple .csv files from cell array in matlab

I have a cell array. I want to write each element of the cell into a .csv file and also specifically name the file along the way.
This is my attempt:
for i=1:length(somecell)
doublecell{i}=double(somecell{i});
end
for j=1:length(doublecell)
z=doublecell{j};
csvwrite('matrix_%i.csv',z,j)
end
I hope what I'm attempting to do is clear even though it's wrong.
You can shorten (and correct) your code as:
for i = 1:length(somecell)
doubleVal = double(somecell{i});
csvwrite(sprintf('matrix_%i.csv', i), doubleVal);
end
You don't have to store the double values in an intermediate cell array, as you can produce the elements while you write the CSV files.
There were actually two problems with your code:
The line z=doublecell(j) produces a cell as indexing a cell-array with parenthesis produces a cell. You would need the numeric value instead, so here the curly bracket indexing would be correct: z = doublecell{j}.
The line csvwrite('matrix_%i.csv',z,j) is incorrect. You would need sprintf to create the filename (see example).

"some_variable = []" type of statement in matlab

Hello and thanks for the help,
I have seen that you can make this statement:
some_variable = []
from what I see, this makes like a void variable for later use.
The thing is I want to make it an array (and that is the problem). I made this
some_variable(:) = []
but there is an error:
In an assignment A(:) = B, the number of elements in A and B must be the same
Thanks
Actually everything in MATLAB is an array, sometimes the arrays are just 1x1, but they are arrays nevertheless (unlike in C/C++ where you have int or int*).
So, when you do var=[] you initialize an empty array, an array of size 0*.
After that, its up to you to initialize it with whatever size you want. var=0 will initialize it as an array of 1x1 size, but you can go bigger, using zeros(size).
Adittionally, if you want to create empty variables of another class, just use <classname>.empty, such as in var=uint32.empty;
*Note that in MATLAB, an array is also infinite dimensional. It's not size 0, its 0x0x0x0x0x.....x0x0. If you want to prove this, try size(var,999999999999) in MATLAB.

How do I create a uninstantiated array of images in MATLAB

I'm looking for a way to store a set of images in my MATLAB function. Is there a way to set aside space for n images ahead of time and initialize them later?
I tried doing something like this:
array_of_images = zeros(1,5); % array of 5 images
but when I try to initialize:
array_of_images(1,1) = imread('image_01.jpg');
MATLAB reports Assignment has more non-singleton rhs dimensions than non-singleton subscripts, so I am looking for a way to do something equivalent.
Check the output of imread('image_01.jpg'), this propably something like 1200x800x3.
Then you have to preallocate ar=zeros(1200,800,3,5) and assign with ar(:,:,:,index)=imread('image.jpg')

Matlab: Matrix of ClassificationKNN class objects

For classification I'm building a number of models for a classifier in MATLAB. I use the class ClassificationKNN for this.
I would very much like to store multiple models (or objects of this class) inside a matrix.
Normally you could access and create matrices inside a matrix with the curly braces ({}).
My loop looks like this:
models = []
for i = 1:length(x)
models = [models, {ClassificationKNN.fit(x,y)}]
end
Unfortunately this returns a matrix models of size (1,3) but all cells are empty which means the models are lost...
How can I make sure every model is stored in a matrix? I need to do this because I need all models later in my calculations and the position in the matrix is important...
Any ideas?
You want a cell array of models, right? It sure looks that way, if that will work try this:
models = {}
for ii = 1:length(x)
models = [models, {ClassificationKNN.fit(x,y)}]
end
Also, you loop through calling ClassificationKNN.fit(x,y) with the same arguments every time, is this just a test, or pseudo-code for an example. Like the comment says, it's best to preallocate like:
models = cell(length(x),1);
for ii = 1:length(x)
models{ii} = ClassificationKNN.fit(x,y);
end
But, either way is likely fine.
Thanks to macduffs post I finally figured out what was going on. Whilest reading his proposition I realised that that indeed should be the correct way if getting a cell array of objects.
After trying it, the array again seemed empty when opening it in the variable editor. I tried calling the first cell in the array to see if it was indeed empty and it was not. It returned the object I had stored in it. This means the question was answered.
I then reverted back to my own method to see if that worked as well and it did. When calling a cell it also returned an object.
Bottom line:
Do not trust the variable editor ^^.