transform the number cell so that only numbers (with or without decimals) excluding zeros are visible

5 vues (au cours des 30 derniers jours)
Alberto Acri le 15 Sep 2023
Commenté : Stephen23 le 15 Sep 2023
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 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
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).

Connectez-vous pour commenter.

Réponse acceptée

Matt J le 15 Sep 2023
string(N)
ans = 8×8 string array
"100" "100" "100" "100" "100" "100" "100" "100" "100.01" "100.01" "100.01" "99.99" "100.01" "100.01" "100.01" "99.98" "42.35" "47" "46.84" "45.65" "47.01" "47.01" "46.85" "45.65" "57.65" "53" "53.16" "54.35" "52.99" "52.99" "53.15" "54.35" "100" "100" "100" "100" "100" "100" "100" "100" "3.7" "4.35" "3.38" "3.59" "4.34" "4.34" "3.37" "3.57" "53.95" "48.65" "49.78" "50.76" "48.65" "48.65" "49.78" "50.78" "57.65" "53" "53.16" "54.35" "52.99" "52.99" "53.15" "54.35"
3 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
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).

Connectez-vous pour commenter.

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.
format shortG
disp(N)
{[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.01]} {[100.01]} {[100.01]} {[99.99]} {[100.01]} {[100.01]} {[100.01]} {[99.98]} {[ 42.35]} {[ 47]} {[ 46.84]} {[45.65]} {[ 47.01]} {[ 47.01]} {[ 46.85]} {[45.65]} {[ 57.65]} {[ 53]} {[ 53.16]} {[54.35]} {[ 52.99]} {[ 52.99]} {[ 53.15]} {[54.35]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 3.7]} {[ 4.35]} {[ 3.38]} {[ 3.59]} {[ 4.34]} {[ 4.34]} {[ 3.37]} {[ 3.57]} {[ 53.95]} {[ 48.65]} {[ 49.78]} {[50.76]} {[ 48.65]} {[ 48.65]} {[ 49.78]} {[50.78]} {[ 57.65]} {[ 53]} {[ 53.16]} {[54.35]} {[ 52.99]} {[ 52.99]} {[ 53.15]} {[54.35]}
1 commentaireAfficher AucuneMasquer Aucune
Alberto Acri le 15 Sep 2023
I want to create a new cell with numbers like the number 100 circled in green (obviously it depends on whether that number has one or two numbers after the decimal point).

Connectez-vous pour commenter.

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.
N, %original
N = 8×8 cell array
{[ 100]} {[100.0000]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.0000]} {[100.0000]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9900]} {[100.0100]} {[100.0100]} {[100.0100]} {[ 99.9800]} {[ 42.3500]} {[ 47]} {[ 46.8400]} {[45.6500]} {[ 47.0100]} {[ 47.0100]} {[ 46.8500]} {[ 45.6500]} {[ 57.6500]} {[ 53.0000]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[ 54.3500]} {[100.0000]} {[100.0000]} {[100.0000]} {[ 100]} {[100.0000]} {[100.0000]} {[100.0000]} {[100.0000]} {[ 3.7000]} {[ 4.3500]} {[ 3.3800]} {[ 3.5900]} {[ 4.3400]} {[ 4.3400]} {[ 3.3700]} {[ 3.5700]} {[ 53.9500]} {[ 48.6500]} {[ 49.7800]} {[50.7600]} {[ 48.6500]} {[ 48.6500]} {[ 49.7800]} {[ 50.7800]} {[ 57.6500]} {[ 53.0000]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[ 54.3500]}
N=cellfun(@(z)round(z,2), N, 'uni',0) %rounded
N = 8×8 cell array
{[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9900]} {[100.0100]} {[100.0100]} {[100.0100]} {[99.9800]} {[ 42.3500]} {[ 47]} {[ 46.8400]} {[45.6500]} {[ 47.0100]} {[ 47.0100]} {[ 46.8500]} {[45.6500]} {[ 57.6500]} {[ 53]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[54.3500]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 100]} {[ 3.7000]} {[ 4.3500]} {[ 3.3800]} {[ 3.5900]} {[ 4.3400]} {[ 4.3400]} {[ 3.3700]} {[ 3.5700]} {[ 53.9500]} {[ 48.6500]} {[ 49.7800]} {[50.7600]} {[ 48.6500]} {[ 48.6500]} {[ 49.7800]} {[50.7800]} {[ 57.6500]} {[ 53]} {[ 53.1600]} {[54.3500]} {[ 52.9900]} {[ 52.9900]} {[ 53.1500]} {[54.3500]}
0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by