www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgsurfview/@app/export.m

    function export(obj,filename)
%EXPORT Export the surface viewer data
%
%  EXPORT(OBJ, FILE) saves a file of comma-separated values containing all
%  the currently displayed data.  Prompts the user for a file name if one
%  is not supplied.

%  Copyright 2000-2005 The MathWorks, Inc. and Ford Global Technologies, Inc.


pname=[];
if nargin<2 || isempty(filename)
	% Prompt the user for a file name
	[filename,pname]=uiputfile('*.csv' , 'Output to Comma-Separated-Values', ...
        mbcGetPath('cage', 'Data'));
	if isnumeric(filename) || isnumeric(pname)
		return;
	elseif ~any(filename=='.')
		filename=[filename '.csv'];
	end
end

[fid, message] = fopen([pname filename],'w');
if fid == -1
    h=errordlg(['File opening/creation failed with error: ' message] , 'Cage' , 'modal');
    uiwait(h);
    return;
end

msgID=addMessage(obj.hStatusBar,'Saving...');

ptrID=obj.PR.stackSetPointer(obj.fig,'watch');

% wrap the next section in a "try" block so that even if there is an
% error, the mouse cursor and the status bar text get changed back
% to their previous values.
try
	% print variable order
	vars=obj.hInputList.variables;
	if length(vars)>=2
		var1=vars(1).getname;
		var2=vars(2).getname;
		fprintf(fid, ['Tables variables: ' var1 ' , ' var2 '\n\n']);
	end

	% The information from the data selector is node-independent.
	datasel=obj.hDataSel.selection;
	if isempty(datasel) | datasel(1)==0
		return;
	end
	datasel=datasel(1); % we only save the first selection

	nodeindices=obj.hNodeSel.selection;
	for k=1:length(nodeindices)
		nodeptr=obj.hNodeSel.nodes(nodeindices(k));
		
		data = obj.calculate( nodeptr );
		if ~isError(data)
			data = getValues(data,1); % only plot the first set of values
			inputs=nodeptr.getinputs;

			% write title
			outputs=nodeptr.getoutputs;
			% the data name is the same for all nodes, but we need a node instance to get it
			dataname=outputs{datasel,1};
			nodename=nodeptr.fullname;
			fprintf(fid, [dataname ' output for ' nodename '\n\n']);

			% write inputs
			fprintf(fid,'Inputs are:\n\n');
			val=[];
			name=[];
			for j=1:length(inputs)
				name = inputs(j).getname;
				val=inputs(j).get('value');
				fprintf(fid,[name '\n   ']);
				i_WriteVector(fid,val);
				fprintf(fid,'\n\n');
			end

			% write data
			fprintf(fid, 'Output is:\n\n');

			if length(vars)==3
				numtables=size(data,3);
				for j=1:numtables
					fprintf(fid, [name '=' num2str(val(j)) '\n'] );
					i_WriteTable(obj,fid,data(:,:,j));
					fprintf(fid,'\n\n');
				end
			else
				i_WriteTable(obj,fid,data);
			end
		end
	end
end

fclose(fid);

obj.PR.stackRemovePointer(obj.fig,ptrID);
removeMessage(obj.hStatusBar,msgID);


%----------------------------
function i_WriteTable(obj,fid,data)

for k=1:size(data,2)
	i_WriteVector(fid,data(:,k));
	fprintf(fid,'\n');
end

%------------------------------
function i_WriteVector(fid,data)
for i=1:length(data)
	% to get commas between entries, we need to do it ourselves
	fprintf(fid, [ num2str(data(i)) ', ']);
end