MATLAB: Error in array2table - matlab

I'm not able resolve this error i face when running MATLAB's array2table function
The VariableNames property must be a cell array, with each element
containing one nonempty character vector.
array2table([1,2,3],'VariableNames',{"str1", "str2", "str3"})
I had a read at MATLAB's array2table documentation and my syntax mimick's MATLAB's give examples, hence i can't figure out where i went wrong. Would appreciate some help.

Rephrasing the error message: String and character arrays are different things. You're using cells of strings but the function expects you to use cells of characters. i.e.
array2table([1,2,3],'VariableNames',{'str1', 'str2', 'str3'})
Relevant documentation for further understanding: Characters and Strings

Related

How to use a FOR loop in a 'struct' subindex? - MATLAB

I have a struct as shown in picture, and I need to address one of the columns in a FOR loop, as shown. But I keep getting this error:
Function 'subsindex' is not defined for values of class 'struct'.
Error in analisa_arx_teste (line 351)
In my case, what i want is :line 1 represents i = 1, line 2 i =2; So, for features, When I ask for pref_estemod(i).features is to get the values from the field features associated to each model.
I am just starting programming with matlab, so all your help would be appreciated.
Thanks!
for i=pref_estemod(1:npreferred)
[m,n]=size(Training);
features=(pref_estemod(1,i).features);
end
The error lies in i=pref_estemod(1:npreferred).
If you intend to use i for indexing, the syntax is for i=1:npreferred.
1:npreferred itself expands to the horizontal array [1,2,...,npreferred]. = with a leading for is a special syntax combination. It means do the following code with i=1, i=2, ..., i=npreferred. Now I am sure you already know the idea behind for loop. The reason I write all this is to give you the following warning/advise. Do NOT expect the same syntax to work with non-numeric arrays. Because it works in some cases and not others.

Octave - Convert number-strings from CSV import file to numerical matrix

I'm writing a code to import data from a CSV file and convert it into an Octave matrix. The imported data can be seen in the following snap:
In the next step I added the following command to delete the commas and "":
meas_raw_temp = strrep(meas_raw_temp,',',' ')
And then I get the data format in the following form:
The problem is that Octave still sees the data as 1 single 1-dimensional array. i.e., when I use the size command I get a single number, i.e. 2647. What I need to have is a matrix output, with each line of the snaps being a row of the matrix, and with each element separated.
Any thoughts?
Here's what's happening.
You have a 1-dimensional (rows only) cell array. Each element (i.e. cell) in the cell array contains a single string.
Your strings contain commas and literal double-quotes in them. You have managed to get rid of them all by replacing them in-place with an 'empty string'. Good. However that doesn't change the fact that you still have a single string per cell.
You need to create a for loop to process each cell separately. For each cell, split the string into its components (i.e. 'tokens') using ' ' (i.e. space) as the delimiter. You can use either strsplit or strtok appropriately to achieve this. Note that the 'tokens' you get out of this process are still of 'string' type. If you want numbers, you'll need to convert them (e.g. using str2double or something equivalent).
For each cell you process, find a way to fill the corresponding row of a preallocated matrix.
As Adriaan has pointed out in the comments, the exact manner in which you follow the steps above programmatically can vary, therefore I'm not going to provide the range of possible ways that you could do so, and I'm limiting the answer to this question to the steps above, which is how you should think about solving your problem.
If you attempt these steps and get stuck on a 'programmatic' aspect of your implementation, feel free to ask another stackoverflow question.

Issue when writing matrix to file using csvwrite

I am trying to output the matrix to a CSV file (comma separated) using this function csvwrite('myMatrix.dat',L); ( where L is square matrix) I got this error:
>> csvwrite('myMatrix.dat',L);
Error using sprintf
Function is not defined for sparse inputs.
Error in dlmwrite (line 169)
str = sprintf(format,m(i,:));
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Kindly, what's wrong with this?
This answer was to answer OP's original error from using:
csvwrite(string('myMatrix'),L);
Error using csvwrite (line 30)
FILENAME must be a character vector.
The error you're seeing is an issue with your input (arguments). It's telling you that the file name must be a "character vector".
According to Matlab's documentation:
There are two ways to represent text in MATLABĀ®. Starting in R2016b, you can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.
http://mathworks.com/help/matlab/matlab_prog/creating-character-arrays.html#briuv_1-1
In simple words. The wrong type was being used as an argument.
To provide a hint for debugging the new error.
Limitations
csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument.
csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite.
http://mathworks.com/help/matlab/ref/csvwrite.html?requestedDomain=www.mathworks.com
Try checking what's in L. whos('L') would also help you get more info on it. An easy way to view what's in your variables, is double click from the workspace. Another way is by creating a break point on your call to csvwrite within a script, then use the debugger and calling for L once you know it's loaded into memory. If you still don't know what's going on, then try 'step in' line by line.
csvwrite does not accept cell arrays.

Passing Struct elements of variable lengths into a matrix Matlab

I am parshing a few data via the internet. The struct Data has several elements. I am interested in Data.Value a call of Data(1,1).Value is a double vector of [56,1]. Moving on to the second struct cell Data(1,2).Value is a double vector [46,1].
Writing a FOR Loop to get the entire Data(1,i).Value from 1 to 500, when it come to the second element, I get the following error returned: Subscripted assignment dimension mismatch.
Although I understand the error I cannot justify it and hence I cannot work out a solution.
I have also tried to pre-define a matrix of variable sizes to overcome this, without result.
Anybody can think of any solution to get the entire Data(1,:).Value
Thanks a lot for the contribution guys.
You can use
vertcat(Data(1,:).Value)
to create a column vector made by concatenating Data(1,1).Value, Data(1,2).Value, ...
Alternatively, you can use the generalized concatenation operator
cat(1, Data(1,:).Value)

Summing the image matrix matlab

I am new to matlab . Can someone please tell me whats wrong with this snippet for summing the 3-D array of image.Its showing error in 3rd line and I am unable to debug it.
x=imread('test.jpg');
imshow(x);
sumdiff=sum(sum(sum(testArr2, 3),2),1)
The only thing that strikes me as obviously wrong is that you are summing over the values in a variable called testArr2 but have the image pixel data in a variable called x. Where does testArr2 get defined and populated with data ?
While Mark had probably answered the question, I wanted to add that the easiest way to sum over the entire array is probably using the colon syntax:
sum_all = sum(x(:))
Also note that imread usually returns an array of integers (uint8 for standard jpeg images). Not all mathematical operations are allowed when using this type of arrays - and sometimes using im2double is necessary.