www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ChangeStructureFieldValueExample.m
%% Change Structure Field Value %% % Create a structure with fields named |'File'| and |'Format'|. S = struct('File',{'myGraph'},'Format',[]) %% % Short-circuit expressions are useful in |if| statements when you want % multiple conditions to be true. The conditions can build on one another % in such a way that it only makes sense to evaluate the second expression % if the first expression is true. % % Specify an |if| statement that executes only when |S| contains an empty % field named |'Format'|. if isfield(S,'Format') && isempty(S.Format) S.Format = '.png'; end S %% % The first condition tests if |'Format'| is the name of a field in % structure |S|. The second statement then tests whether the |Format| field % is empty. The truth of the second condition depends on the first. The % second condition can never be true if the first condition is not true. % Since |S| has an empty field named |'Format'|, the body statement % executes and assigns |S.Format| the value |'.png'|.