Matlab - Text file names from Cell array - matlab

I've made a cell array that goes through the names of 50 text files I need to analyze. However, when referencing a particular character element as the title for textread, I get an error.
Example:
NameCell = {'file1.txt' 'file2.txt'};
vec = textread(NameCell{1},'%n','headerlines',23);
If I input the actual file name ('file1.txt') into the textread function, I get the vector I want, but it can't reference from the cell. Is there a way around this?
Thank you!

Related

Can i write out a txt or csv doc with data of varying dimensions in Matlab?

I am using Matlab R2013b.
I have a 100x100 matrix which contains both numbers and strings. I converted it to a cell array (alldat) and wrote it to a csv file (blah.csv).
I then tried to append a single number to the top line of this csv file...which Matlab won't let me do.
cell2csv('blah.csv',alldat)
I can append the single number 'n' at the bottom of the matrix:
dlmwrite('blah.csv',n,'-append','delimiter',' ','roffset',1)
But it won't let me do it the other way around (so I can put the number in the first cell of the csv file, then have the matrix below it.
Can anyone advise?
I also tried outputting the cell array to a txt document using dlmwrite:
dlmwrite('blah.txt',alldat,'delimiter',' ');
And I kept getting this error:
Error using dlmwrite (line 113) The input cell array cannot be
converted to a matrix.
I often use tables for such tasks. Since you have a 100 x 100 array and not variables with different dimensions, it should be possible to adapt.
VarA={'12A3';123;'12B3'};
VarB={'45A6';456;'45B6'};
T=table(VarA,VarB);
writetable(T,'test.csv','WriteVariableNames',false)
T1=readtable('test.csv','ReadVariableNames',false)
You may want to use cell2table to create a table directly from your cell array, although it didn't work for me because it made some strange conversions from number to character.

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

save part of matlab cell array, proper refence

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

Setting Column and Row Names from Cell in uitable - MATLAB

I am using GUIDE to build my first GUI interface in MATLAB.
I have several matrices I want to display using the uitable. For now let's focus on one matrix, say myMatrix [10x5]
Now I have two cells of strings, columnNames (1x5), and another, rowNames (10x1). I would like to set these cells to the row and column names of the table, but I cant yet figure out how to do this.
The MATLAB help page says you can use a cell of strings to do this, however in the property inspector, and under ColumnName, the only non-numeric option is to enter the names manually.
Any help would be appreciated (or suggestions to go about this in a different way).
In order to have custom Row/Column Names you have to pass a cell of strings (using {<names>}) into the ColumnName and RowName properties of the uitable. Here is an example directly from MatLab's uitable documentation:
f = figure('Position',[200 200 400 150]);
dat = rand(3);
cnames = {'X-Data','Y-Data','Z-Data'}; % These are your column names
rnames = {'First','Second','Third'}; % These are your row names
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,...
'RowName',rnames,'Position',[20 20 360 100]);
When parsing you're file, be sure to create the lists as a cell of strings.

How to convert Matlab string CSV of cell into separate columns of a matrix?

If I have a Matlab CELL line of string characters separated by a comma like:
12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C
How do I separate each one into its own column within a matrix? What is the most efficient way of doing this?
Can this be done?
Thanks
UPDATE: Just realized this question duplicates how-to-convert-comma-separated-string-to-cell-array-of-strings-in-matlab and that my answer duplicates the answer provided by #Jonas. I'm flagging it as a duplicate.
Here is the most efficient method I'm aware of:
%# Build your string
Input = '12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C';
%# Convert to cell array of strings
Output = regexp(Input, '([^,]*)', 'tokens');
Output = cat(2, Output{:});
Some points to note:
1) Note, this will work whether Input is a character array (ie a string) or a cell containing a character array.
2) I've assumed you want the output to be a cell array of strings. Offhand, I can't think of another way of grouping strings of differing length into separate columns using Matlab without building your own custom class.
3) The solution provided by #IlyaKobelevskiy will group the strings into separate rows (not columns) and thus essentially limits you to only a single observation.
Assuming str is your cell line:
str ={'12/28/2012,00:00:01,0.99458,1,10518,0.99458,0.99483,0,0,0,0,-,-,-,b,-,C'};
ind=strfind(str{1},',');
len=diff([0 ind]);
sz=max(len);
A=zeros(length(ind),sz);
prev=1;
for i=1:length(ind)
A(i,1:len(i)-1)=str{1}(prev:(ind(i)-1));
prev=ind(i)+1;
end;
A=char(A);
Not sure if it is the most efficient way, but should work...