transform the number cell so that only numbers (with or without decimals) excluding zeros are visible
조회 수: 4 (최근 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.

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에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!