transform the number cell so that only numbers (with or without decimals) excluding zeros are visible
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Matt J
le 15 Sep 2023
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
le 15 Sep 2023
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).
Réponse acceptée
Matt J
le 15 Sep 2023
load N
string(N)
3 commentaires
Matt J
le 15 Sep 2023
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
le 15 Sep 2023
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).
Plus de réponses (2)
Matt J
le 15 Sep 2023
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
le 15 Sep 2023
Modifié(e) : Matt J
le 15 Sep 2023
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 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!