How to write the matrix with blank element?

52 Ansichten (letzte 30 Tage)
suvadip paul
suvadip paul am 4 Okt. 2013
Kommentiert: Ruben am 18 Jul. 2022
for i=1:10
if i~=6 T(i)=2*i
else T(i)= %(i want to leave it blank) end end
T
Is this possible in Matlab? If yes, How to do this?
  2 Kommentare
suvadip paul
suvadip paul am 4 Okt. 2013
if T(6) cannot left blank, then I want to display T(6)= ND (Not defined) so that T= 2 4 6 8 10 ND 14 16 18 20
Rohit Deshmukh
Rohit Deshmukh am 28 Feb. 2020
is there any possibility of keeping the element blank?
I do not want nan or ND nor do i want to delete the element from matrix.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 4 Okt. 2013
Bearbeitet: Image Analyst am 4 Okt. 2013
Just set those elements to nan:
T(i) = nan;
otherwise you'd be looking at a cell array and that's a lot more complicated to use and you probably don't want the hassles of dealing with those. Nan means "Not a number" and is basically the concept of "not defined' that you're trying to achieve. They just call it nan instead of ND.
  8 Kommentare
Steven Lord
Steven Lord am 14 Jul. 2022
It is not possible to have an array in MATLAB (except for a Java array) that has "holes" in it.
For the question about tick labels on an axes where you've plotted a datetime array, that's not too difficult. Here's one with tick labels every day.
v = 0:10;
x = datetime('today') + days(v);
y = v.^2;
plot(x, y)
xticks(x)
title('Labels each day')
Here's one with a tick every day and a tick label every other day.
figure
plot(x, y)
xticks(x)
% Customize how the label strings are formatted
x.Format = 'MMM dd';
s = string(x);
s(2:2:end) = "";
xticklabels(s)
title('Labels every other day')
Ruben
Ruben am 18 Jul. 2022
Thank you @Steven Lord, that solved my issue!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Vidhya Dharshini
Vidhya Dharshini am 4 Okt. 2013
Hi... try this
for i=1:10
if(i~=6)
T=2*i
else
T='ND'
end;
T
end
just give T='ND' and it will display.
  1 Kommentar
Image Analyst
Image Analyst am 4 Okt. 2013
But you're overwriting T every time, plus sometimes it's a double and other times it's a character string.

Melden Sie sich an, um zu kommentieren.


Ruben
Ruben am 14 Jul. 2022
Bearbeitet: Ruben am 14 Jul. 2022
Thanks for getting back to me!
Like @Rohit Deshmukh, I would like to have blank elements in my vector (or maybe it's a "datetime array"?)
I'm plotting millions of lines of data in monthly chunks. For my plots I want xticks for every day but xtick labels only every other (second) day. In other words I want minor tickmarks every day, and major tickmarks every other day (I know there is the XMinorTick 'on' option, but I haven't figured that out yet so I'm also trying this strategy).
Below, I'm trying to include these blanks in my array, but instead of reading datetime([],[],[]) as a physical blank space, it just skips over it, leaving me with only the major tickmarks.
% data is collected every minute
timestartm = datetime(2022,02,01);
timeendm = datetime(2022,02,28);
xlimit = [timestartm timeendm];
xtki = 2; % xtick interval is 2
xtkl = [(timestartm) (datetime([],[],[])) (timestartm + xtki) (datetime([],[],[])) (timestartm + 2*xtki)...
(datetime([],[],[])) (timestartm + 3*xtki) (datetime([],[],[])) (timestartm + 4*xtki) (datetime([],[],[]))...
(timestartm + 5*xtki) (datetime([],[],[])) (timestartm + 6*xtki) (datetime([],[],[]))...
(timestartm + 7*xtki) (datetime([],[],[])) (timestartm + 8*xtki) (datetime([],[],[]))...
(timestartm + 9*xtki) (datetime([],[],[])) (timestartm + 10*xtki) (datetime([],[],[]))...
(timestartm + 11*xtki) (datetime([],[],[])) (timestartm + 12*xtki) (datetime([],[],[]))...
(timestartm + 13*xtki) (datetime([],[],[])) (timestartm + 14*xtki) (datetime([],[],[]))]

Image Analyst
Image Analyst am 14 Jul. 2022
You're posting this as an "Answer" to a question that people can see (by the green box) that has already been successfully answered. So many people will not open this and look at your question. Again it's best if you start a new question - not anywhere here in this discussion thread, but a brand new question all your own. More people will see it and answer it then.
If you can mock up some desired output, whether it's some numbers or a picture of something you drew in Photoshop, such as a plot or whatever. But people need to see what you'd like to achieve as a solution.
  1 Kommentar
Ruben
Ruben am 18 Jul. 2022
Oh I understand now. Thanks for explaining that to me and sending me the tutorial.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by