1

I'm gathering information from calculations performed on some data and stored into arrays. I also have some info about these data coming from a text file which now and then contains strings.

The strings from the text files got saved into a {} cell array of strings such as:

strings={'s1' 's2' 's3'};
a=[1 2 3]

What the strings and arrays contain is generated based on a few conditionals from the data present in the text file as well as some data I have in matlab through a loop doing things like that:

srings{e}=blablahFromSomewhere{e}
a(e)=otherNumericalBlahBlahFromSomwehre(e+6)

Ultimately I want to joint this into a table. I would normally do this:

T=[a(:) strings(:)]

But I'm facing the following error:

Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Can anyone help? I don't really want to transform the strings into integers because the content of the string is handier to have in the output in running the analysis.

Thanks :)

3 Answers 3

1

Code

strings={'s1' 's2' 's3'};
a=[1 2 3];
outputfile = 'output.txt';

%%// Code to horziontally concatenate to result in a Nx2 cell array
out = [num2cell(num2str(a,'%d')') strings']

%%// Write to outputfile - Method 1
out = out';
fid = fopen(outputfile,'w');
fprintf(fid, '%s\t%s\n', out{:});
fclose(fid);

%%// Write to outputfile - Method 2
%%// Create a text file and clear it out of any content. This is needed, as otherwise
%%// XLSREAD was initializing CSV files with weird characters
%% dlmwrite(outputfile,'');

%%// Write to CSV file using XLSREAD
%xlswrite(outputfile,out)

%%// Verify
type(outputfile)

Output

out = 

    '1'    's1'
    '2'    's2'
    '3'    's3'


1   s1
2   s2
3   s3
Sign up to request clarification or add additional context in comments.

6 Comments

Ok THIS is pretty close but then when I use the following dlmwrite(fileName,T,'-append','delimiter','\t'); the string gets split in as many "columns" as there are tabs.
Edited to your new requirements!
Thanks for the reply. It's giving me every character separated by a comma sadly. I also get an error that it could not start Excel. This is turning out to be a headache of a task
It would appear xlswrite is a PC only thing. Downloaded the "fix" for mac developed by third party but I will only save in xls which ultimately isn't really handy since this table has to go to R. Damn MATLAB!
Great! Superb! Thanks so much for your help and patience!
|
1

It's a little unclear what you want, but it would seem to be:

T = table(a(:), strings(:));

Assuming I'm reading the documentation for table correctly.

Or, for a cell array:

C = [num2cell(a(:)) strings(:)];

3 Comments

Thought this would be perfect but then because I'm using dlmwrite to write to a text file I get the following: Error using dlmwrite (line 118) The input cell array cannot be converted to a matrix.
@Bastien I've never used it, but according to the docs dlmwrite only handles numeric data, not strings. If you have new enough Matlab or the stats toolbox, using tables and writetable looks like it might work. More ideas here.
Sadly I don't have write table... I'm just a version under to have that sadface
1

If you want to obtain a char array:

    aux = num2str(a(:));
    aux = mat2cell(aux,ones(1,size(aux,1)),size(aux,2));
    T = cell2mat([aux strings(:)]);

The result is a 2D char array (leading spaces are introduced if needed):

    T =

    1s1
    2s2
    3s3

5 Comments

OP has mentioned - "T=[a(:) strings(:)]", so I think he needs a cell output.
hey @Luis Thanks. It's not quite what I want. The ultimate table should consist of two columns one for a and one for strings
@Bastien So... do you want a 2D-cell array as the result?
I guess a 2D-cell array could do yeah.
although dlmwrite might not like it (see my comment to answer below)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.