How to get the "zero/blank" element of a given variable?

I have an array VAR of unknown type. When the array is extended, Matlab knowns how to fill in blank ("zero") values of the correct type. How do I get this default blank value for any given Var type?
One solution is simply to extend an array and see what value gets padded:
BLANK(2) = VAR(1) ; BLANK(2) = [] ;
This returns a scalar BLANK with the correct "0" value and works for any VAR type.
BUT: It doesn't work with empty VAR.
Any thoughts of a general way to get the Blank value for any given array even if empty?

 Akzeptierte Antwort

Rik
Rik am 29 Aug. 2019
Bearbeitet: Rik am 29 Aug. 2019
You can abuse repmat for this:
a=uint8(8);
repmat(a,0,0)
b='foo_bar';
repmat(b,0,0)
Edit:
On second read, you might actually mean this instead:
a=uint8(8);b='foo_bar';c={'1',2};
get_blank_val=@(x) eval([class(x) '(0)']);
get_blank_val(a)
get_blank_val(b)
get_blank_val(c)

7 Kommentare

royk
royk am 29 Aug. 2019
thanks. this is an elegant way to get an empty array of a given type. but what i am looking for is getting a scalar "zero" of the given type.
Say, for a=uint8(8) I would like to get BLANK = uint8(0) ;
for a='foo_bar', BLANK = ' ' ; (single space)
for a={10,20}, BLANK = {[]} ;
etc
While you comment suggests the blank for a char is a space, a quick test on R2015a shows it is a char(0). That might have changed in later releases though. A quick way to check is this:
bb='foo';
bb(8)='y';
disp(double(bb))
royk
royk am 29 Aug. 2019
yes, you are right it is char(0) indeed
But, what is then the general way of getting this Blank value for any variable type? Especially how do I get the blank/zero value corresponding to a given empty variable?
Rik
Rik am 29 Aug. 2019
My edited answer should give you that. I admit the use of eval is suboptimal, but this strategy supports a wider range of options than the zeros function (e.g. the cell class) and it should also support custom classes. It also works on more releases than zeros(1,'like',x) would.
very nice! much thanks!
it works quite well, but for instance, won't work on structs. (it throws an error).
Yet, the “0” for struct seems well defined within Matlab:
>> Var = struct('a',3,'b',4) ;
>> Var(3) = Var(1) ;
>> Var(2)
ans =
struct with fields:
a: []
b: []
You could put in a special condition for a struct, but then you can't make it an anonymous function anymore.
function blank=get_blank_val(x)
if ~isa(x,'struct')
blank=eval([class(x) '(0)']);
else
fn=fieldnames(x);
tmp=[fn repmat({[]},size(fn))]';
blank=struct(tmp{:});
end
end
royk
royk am 30 Aug. 2019
that will do the job
there are still other exceptions. for example, for an array of MyClass objects, Matlab determines Blank for array extension by calling empty(MyClass).
It thereby feels like there got to be a generic Matlab function that returns the Blank object for array extension for ANY type.
Anyway, your kind suggestion above surely does the job for my needs.
thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Gefragt:

am 29 Aug. 2019

Kommentiert:

am 30 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by