Cell Array to output like this?

A = 14×1 cell array
{'0.1' }
{'0.2' }
{'1' }
{'1.1' }
{'1.2.3' }
{'1.2.3.1'}
output i want like this
A =
0.1
0.2
1
1.1
1.2.3
1.2.3.1

2 Commenti

Rik
Rik il 11 Ago 2021
What is flagging your own question going to achieve? You can post comments and even edit your question to clarify it.

Accedi per commentare.

 Risposta accettata

Here is one way:
A = {'0.2','1'}
A = 1×2 cell array
{'0.2'} {'1'}
output = cellfun(@str2num,A)
output = 1×2
0.2000 1.0000

8 Commenti

A = {'0.2','1.2.3'}
output = cellfun(@str2num,A)
Error using cellfun
Non-scalar in Uniform output, at index 2, output 1.
Set 'UniformOutput' to false.
the cyclist
the cyclist il 11 Ago 2021
Modificato: the cyclist il 11 Ago 2021
Oh, sorry. Missed that you had non-numbers in there.
1.2.3.1 is not a number, and cannot be stored as anything other than a string or character array.
What are you trying to do as a next step, where you want "only the number"? To be clear, the single quotes that MATLAB puts around each character array are not part of the array itself. It is only a notation.
Divyesh pandav
Divyesh pandav il 11 Ago 2021
Modificato: Divyesh pandav il 12 Ago 2021
i want passing this value in this query
query = strcat('SELECT sum(abc) from abc = ''',A ,''' ORDER BY abc ');
the cyclist
the cyclist il 11 Ago 2021
Modificato: the cyclist il 11 Ago 2021
Hm. It seems to me that you actually want a string -- not a MATLAB numeric value -- in order to be able to build the query. Can you post an example of what the query would be if you were using just SQL (and not creating it in MATLAB)? Don't include ANY additional quotes that would not be in the SQL. For example, I am guessing it is something like
====================================================================
SELECT SUM(abc.time_taken) FROM abc WHERE abc.que_toc_no = '1.2.3.1'
====================================================================
Then maybe we can see how to construct that sequence of characters, using the contents of the cell array.
yes, you are right , how to get this value in cell array?
A = {'0.2','1.2.3'};
query = cellfun(@(x) strcat('SELECT sum(abc .time_taken) from abc.que_toc_no = ''',x ,''' ORDER BY abc '),...
A,'UniformOutput',false);
query.'
ans = 2×1 cell array
{'SELECT sum(abc .time_taken) from abc.que_toc_no = '0.2' ORDER BY abc' } {'SELECT sum(abc .time_taken) from abc.que_toc_no = '1.2.3' ORDER BY abc'}
thank you so much
No need for CELLFUN when STRCAT can handle that cell array by itself:
A = {'0.2','1.2.3'};
B = strcat('SELECT sum(abc .time_taken) from abc.que_toc_no = ''',A(:),''' ORDER BY abc')
B = 2×1 cell array
{'SELECT sum(abc .time_taken) from abc.que_toc_no = '0.2' ORDER BY abc' } {'SELECT sum(abc .time_taken) from abc.que_toc_no = '1.2.3' ORDER BY abc'}

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by