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

 采纳的回答

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 个评论

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.
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.
i want passing this value in this query
query = strcat('SELECT sum(abc) from abc = ''',A ,''' ORDER BY abc ');
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'}

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by