How to combine an cell array and a double array

i am trying to convert a cell array (consisting of strings) into a double array in order to be able to combine it with a double array (matrix) (consisting of numbers)using str2double but it gives me NaN? is there another solution?
[EDITED, Jan Simon, important information from a comment]:
Example:
x = {'USGG3M Index' 'USGG6M Index' 'USGG9M Index'}
how to convert it to a double matrix?

5 commentaires

Jan
Jan le 22 Mar 2012
Please post an explicite example.
yasmine
yasmine le 22 Mar 2012
example: x = {'USGG3M Index' 'USGG6M Index' 'USGG9M Index'}
how to convert it to a double matrix?
Jan
Jan le 22 Mar 2012
Dear yasmine: How do you want to convert 'USGG3M Index' to a double? What is the wanted result? NaN is the expected result as explained in "doc str2double".
Geoff
Geoff le 22 Mar 2012
cellfun( @(s) str2double(regexp(s,'\d+','match')), x ) ???
yasmine
yasmine le 25 Mar 2012
but this returns the number 3 only not the whole string USGG3M Index

Connectez-vous pour commenter.

Réponses (3)

Jan
Jan le 25 Mar 2012
You cannot insert strings in a double array in Matlab. A double array consists of doubles, as the name says already.
A cell can contain elements of different types:
C = {'Header1', 'Header2'; ...
17, 8.15};
But of course C is not a double array anymore. The usual method to store numerical arrays and names for the columns is using two different variables.
Your question could not be answered sufficiently for three days now, because you do not post the required details inspite of repeated questions for clarifications. This is inefficient.

5 commentaires

yasmine
yasmine le 28 Mar 2012
Thanks a lot Jan for your time.
Tao Bi comments to Jan Simon:
Good explanation for how to store the numerics and the strings
If I store values and column names in two different variables, is it in some way possible to combine both when saving to a txt file?
Here is an example:
header = {'m1','m2','m3'};
values = zeros(nSamples,3);
values(:,1) = m1;
values(:,2) = m2;
values(:,3) = m3;
here I want to combine both to a variable 'header_values'
save('output.txt','header_values','-ascii');
Thx for your answers
save() supports -append that can add more to the end of a text file.
However, save -ascii does not support cell array of character vectors, and if you try to save -ascii of a plain character vector then it will convert the characters to numbers.
dlmwrite() can write character vectors, but you have to abuse its 'precision' option pretty badly to do that.
The realistic options are:
  1. fopen() / fprintf() the header / fclose, after which you can save -ascii of just the numbers
  2. fopen() / fprintf() everything / fclose, which can produce any text format you want
  3. Use a table() object with the headers as the variable names, and writetable()
  4. Convert everything into a cell array, one header or one number per cell, and use writecell()
These days I would typically use writetable() unless I had specialized output format needs; if I had specialized needs then tricks like dlmwrite() are just not worth it, and fprintf() with a custom format is best.
Vincent
Vincent le 24 Juil 2020
Thank you for the quick answer. It worked perfectly well first converting the array to a table (array2table) and then save it with writetable(). :)

Connectez-vous pour commenter.

Wayne King
Wayne King le 22 Mar 2012
I think you should give us a very simple concrete example with MATLAB code.
x = {'2','3','4'};
y = cellfun(@str2double,x,'uni',false);
y = cell2mat(y);

2 commentaires

yasmine
yasmine le 22 Mar 2012
i have tried in your example, it worked. However, in the case of my exmaple, it gives me NaN
Wayne King
Wayne King le 22 Mar 2012
Jan's question is right on target, the problem is what kind of number do you think USGG3M is?

Connectez-vous pour commenter.

Jan
Jan le 22 Mar 2012
C = {'3.14159265', '1.414562373095'};
D = sscanf(sprintf('%s,', C{:}), '%g,');
This is still faster than using a C-mex to convert the single strings.

5 commentaires

yasmine
yasmine le 22 Mar 2012
but this does not yield an array for a furtehr purpose to be combined with a double array
Jan
Jan le 22 Mar 2012
No array? What do you get as output?
yasmine
yasmine le 22 Mar 2012
Dear Jan, i have tried the example of Wayne King and it works out for his example, so why it did not work for mine?
Jan
Jan le 22 Mar 2012
Because 'USGG3M Index' is not the string representation of a number, in opposite to '2' or '2.3'. Please explain what you expect as result of converting 'USGG3M Index' to a double. This core point of your question is not clear.
My example is working also, btw.
yasmine
yasmine le 25 Mar 2012
i want to make a new array that contains a cerain output (double array) and has a heading USGG3M Index. accordingly, in order to concatenate (merge) these two array, they have to be double. regards

Connectez-vous pour commenter.

Catégories

Tags

Question posée :

le 22 Mar 2012

Commenté :

le 24 Juil 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by