transform the number cell so that only numbers (with or without decimals) excluding zeros are visible
5 次查看(过去 30 天)
显示 更早的评论
Hi. I want to transform the number cell "N" so that only numbers (with or without decimals) excluding zeros are visible (see for example the number 100 circled in green). Shown are the "transformations" I would like to apply to all numbers inside cell "N". I have tried a for loop but that does not think to be the right way.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1483831/image.png)
N = importdata("N.mat");
% N1 = cell2table(N);
N_new = {};
for K = 1:height(N)
for k = 1:width(N)
aa = N{K,k};
F = compose('%.2f',aa);
end
N_new = [N_new,{F}];
end
2 个评论
Matt J
2023-9-15
see for example the number 100 circled in green
This hints that your post should contain an image for us to look at, but I see none.
Stephen23
2023-9-15
Why are you very inefficiently storing lots of scalar numerics in a cell array?
"I have tried a for loop but that does not think to be the right way."
You are forced to write a loop because you are not storing your data the right way (in a numeric matrix).
采纳的回答
Matt J
2023-9-15
load N
string(N)
3 个评论
Matt J
2023-9-15
That would be a peculiar thing to do, because strings already have cell-like indexing syntax,
load N
Ns=string(N);
Ns{2,1}
However, if you're sure it's what you want,
Nscell=num2cell(string(N))
Stephen23
2023-9-15
It is not recommended to use cell arrays of (scalar) string arrays, because these are very ineffiicient and do not allow any of the benefits of using string arrays:
However it would certainly be okay to have a cell array of character vectors (e.g. using COMPOSE or CELLSTR).
更多回答(2 个)
Matt J
2023-9-15
If your're just looking for a way to change the display format, and not the numbers themselves, I think you can only do that in the Command Window, not the Variables Editor.
load N
format shortG
disp(N)
Matt J
2023-9-15
编辑:Matt J
2023-9-15
You can round all of the numbers to 2 decimal places as below. This will make it so that any number that was an integer to 2 decimal places will be displayed as an integer without any trailing zeros. However, there is no way to get numeric non-integers to display with less than 4 decimal places in the Variables Editor, and not in the Command Window either, except with format shortG or format bank.
load N
N, %original
N=cellfun(@(z)round(z,2), N, 'uni',0) %rounded
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!