www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@calibrationdata/@dcminterface/pWrite.m
function pWrite(obj, calibration, filename) %PWRITE Private method. % PWRITE writes a calibration to a MATLAB file. % % PWRITE(OBJ,CALIBRATION,FILENAME) % % See also COMMIT, PREAD. % Copyright 2000-2011 The MathWorks, Inc. narginchk(3,3); nargoutchk(0,0); % Do not write to empty if isempty(filename) return end pm = obj.ProgressManager; pm.reset; pm.notify('Performing startup tasks'); % Get identifiers valueIdentifiers = calibration.getValueIdentifiers; axisIdentifiers = calibration.getAxisIdentifiers; curveIdentifiers = calibration.getCurveIdentifiers; mapIdentifiers = calibration.getMapIdentifiers; % Since both axes and curves are stored as KENNLINIE, check for duplicates [~, ~, cIndex] = intersect(axisIdentifiers, curveIdentifiers); % Write just once curveIdentifiers(cIndex) = []; % Store lengths for convenience nValues = numel(valueIdentifiers); nAxes = numel(axisIdentifiers); nCurves = numel(curveIdentifiers); nMaps = numel(mapIdentifiers); % Open file f = fopen(filename, 'w'); if f == -1 error(message('mbc:calibrationdata:FileWriteError', filename)); end [~, filename, ~] = fileparts(filename); % Write header fprintf(f, '* DCM File: %s\n', filename); fprintf(f, '* Generated by MATLAB %s on %s.\n\n', version, datestr(now)); % Process values if nValues > 0 pm.notify(sprintf('Retrieving %d values', nValues)); values = calibration.getValue(valueIdentifiers); pm.notify(sprintf('Processing %d values', nValues)); for i = 1:nValues, try % Retrieve thisValue = values(i); % Serialize thisValueText = iValue2Text(thisValue); % Write to file fprintf(f, '%s\n', thisValueText); catch ME error('mbc:calibrationdata:dcminterface:FileWriteError', ... 'Could not write value ''%s''.\n%s', thisValue.Identifier, ME.message) end pm.update(i/(nValues+nAxes+nCurves+nMaps)); end end % Process axes if nAxes > 0 pm.notify(sprintf('Retrieving %d axes', nAxes)); axes = calibration.getAxis(axisIdentifiers); pm.notify(sprintf('Processing %d axes', nAxes)); for i = 1:nAxes, try % Retrieve (only public axes) thisAxis = axes(i); thisAxisIsPublic = strcmp(calibration.pGetAxisIdentifierType(thisAxis.Identifier),'public'); if thisAxisIsPublic % only public % Serialize thisAxisText = iAxis2Text(thisAxis); % Write to file fprintf(f, '%s\n', thisAxisText); end catch ME error(message('mbc:calibrationdata:dcminterface:FileWriteError1', thisAxis.Identifier, ME.message)) end pm.update((i+nValues)/(nValues+nAxes+nCurves+nMaps)); end end % Process curves if nCurves > 0 pm.notify(sprintf('Retrieving %d curves', nCurves)); curves = calibration.getCurve(curveIdentifiers); xAxes = calibration.getAxis(get(curves, {'XAxisIdentifier'})); pm.notify(sprintf('Processing %d curves', nCurves)); for i = 1:nCurves, try % Retrieve thisCurve = curves(i); thisXAxis = xAxes(i); thisXAxisIsPublic = strcmp(calibration.pGetAxisIdentifierType(thisXAxis.Identifier),'public'); % Serialize thisCurveText = iCurve2Text(thisCurve, thisXAxis, thisXAxisIsPublic); % Write to file fprintf(f, '%s\n', thisCurveText); catch ME error(message('mbc:calibrationdata:dcminterface:FileWriteError2', curveIdentifiers{ i }, ME.message)) end pm.update((i+nValues+nAxes)/(nValues+nAxes+nCurves+nMaps)); end end % Process maps if nMaps > 0 pm.notify(sprintf('Retrieving %d maps', nMaps)); maps = calibration.getMap(mapIdentifiers); xAxes = calibration.getAxis(get(maps, {'XAxisIdentifier'})); yAxes = calibration.getAxis(get(maps, {'YAxisIdentifier'})); pm.notify(sprintf('Processing %d maps', nMaps)); for i = 1:nMaps, try % Retrieve thisMap = maps(i); thisXAxis = xAxes(i); thisYAxis = yAxes(i); thisXAxisIsPublic = strcmp(calibration.pGetAxisIdentifierType(thisXAxis.Identifier),'public'); thisYAxisIsPublic = strcmp(calibration.pGetAxisIdentifierType(thisYAxis.Identifier),'public'); % Serialize thisMapText = iMap2Text(thisMap, thisXAxis, thisXAxisIsPublic, thisYAxis, thisYAxisIsPublic); % Write to file fprintf(f, '%s\n', thisMapText); catch ME pm.update((i+nValues+nAxes+nCurves)/(nValues+nAxes+nCurves+nMaps)); error(message('mbc:calibrationdata:dcminterface:FileWriteError3', mapIdentifiers{ i }, ME.message)) end pm.update((i+nValues+nAxes+nCurves)/(nValues+nAxes+nCurves+nMaps)); end end % Close file to commit changes pm.notify('Writing file to disk'); fclose(f); pm.update(1); pm.notify('Cleaning up'); % ------------------------------------------------------------------------- function text = iValue2Text(value) %IVALUE2TEXT Convert a value to text. % % TEXT = IVALUE2TEXT(VALUE) converts a value VALUE to text TEXT. % Header keyword = 'FESTWERT'; header = sprintf('%s %s\n', keyword, value.Identifier); % Metadata metadata = sprintf(' LANGNAME "%s"\n EINHEIT_W "%s"\n', ... value.Description, value.Units); % Data % Value body = iRow2Str(value.Value, 'WERT'); % Footer footer = sprintf('%s\n', 'END'); % Combine text = [header metadata body footer]; % ------------------------------------------------------------------------- function text = iAxis2Text(axis) %IAXIS2TEXT Convert an axis to text. % % TEXT = IAXIS2TEXT(axis) converts an axis AXIS to text TEXT. axisIndex = axis.Index; if false && all(axisIndex==1:numel(axisIndex)) % DISABLED % No skipped breakpoints, axis -> STUETZSTELLENVERTEILUNG text = iAxis2Text_stuetzstellenverteilung(axis); else % Skipped breakpoints, axis -> KENNLINIE text = iAxis2Text_kennlinie(axis); end % ------------------------------------------------------------------------- function text = iAxis2Text_stuetzstellenverteilung(axis) %IAXIS2TEXT Convert an axis to text. % % TEXT = IAXIS2TEXT(axis) converts an axis AXIS to text TEXT. % Header keyword = 'STUETZSTELLENVERTEILUNG'; header = sprintf('%s %s %d\n', keyword, axis.Identifier, numel(axis.Value)); % Metadata metadata = sprintf(' LANGNAME "%s"\n EINHEIT_X "%s"\n', ... axis.Description, axis.Units); % Data % Value body = iRow2Str(axis.Value, 'ST/X'); % Footer footer = sprintf('%s\n', 'END'); % Combine text = [header metadata body footer]; % ------------------------------------------------------------------------- function text = iAxis2Text_kennlinie(axis) %IAXIS2TEXT Convert an axis to text. % % TEXT = IAXIS2TEXT(axis) converts an axis AXIS to text TEXT. % Header if axis.Monospaced keyword = 'FESTKENNLINIE'; else keyword = 'KENNLINIE'; end header = sprintf('%s %s %d\n', keyword, axis.Identifier, length(axis.Value)); % Metadata metadata = sprintf(' LANGNAME "%s"\n EINHEIT_X "%s"\n EINHEIT_W ""\n', ... axis.Description, axis.Units); % Data % Value value = iRow2Str(axis.Value, 'ST/X'); % Value index = iRow2Str(axis.Index-1, 'WERT'); % MATLAB is 1-based, DCM is 0-based % Concatenate body = [value index]; % Footer footer = sprintf('%s\n', 'END'); % Combine text = [header metadata body footer]; % ------------------------------------------------------------------------- function text = iCurve2Text(curve, xAxis, ~) %ICURVE2TEXT Convert a curve to text. % % TEXT = ICURVE2TEXT(CURVE,XAXIS) converts a curve CURVE and corresponding % axis XAXIS to text TEXT. xAxisIsPublic = false; % disable % Retrieve some common properties curveValue = curve.Value; xAxisIndex = xAxis.Index; xAxisValue = xAxis.Value; % Perform basic checks if numel(xAxisValue)<2 error(message('mbc:calibrationdata:dcminterface:InvalidOperation', numel( xAxisValue ))); elseif isempty(curveValue) error(message('mbc:calibrationdata:dcminterface:InvalidOperation1')); end % Are axes simple? (i.e. no skipped breakpoints) xAxisIsSimple = all(xAxisIndex==1:numel(xAxisIndex)) && numel(xAxisValue)==numel(curveValue); % Header if xAxis.Monospaced keyword = 'FESTKENNLINIE'; else keyword = 'KENNLINIE'; end header = sprintf('%s %s %d\n', keyword, curve.Identifier, length(curve.Value)); if xAxisIsPublic header = sprintf('%s*SSTX %s\n', header, xAxis.Identifier); end % Metadata metadata = sprintf(' LANGNAME "%s"\n EINHEIT_X "%s"\n EINHEIT_W "%s"\n', ... curve.Description, xAxis.Units, curve.Units); % Compute breakpoints, interpolating when necessary if ~xAxisIsSimple xAxisValue = interp1(xAxisIndex, xAxisValue, 1:numel(curveValue)); end % Data % X axis axis = iRow2Str(xAxisValue, 'ST/X'); % Value value = iRow2Str(curveValue, 'WERT'); % Concatenate body = [axis value]; % Footer footer = sprintf('%s\n', 'END'); % Combine text = [header metadata body footer]; % ------------------------------------------------------------------------- function text = iMap2Text(map, xAxis, ~, yAxis, ~) %IMAP2TEXT Convert a map to text. % % TEXT = IMAP2TEXT(MAP,XAXIS,YAXIS) converts a map MAP and corresponding % axes XAXIS and YAXIS to text TEXT. xAxisIsPublic = false; % disable yAxisIsPublic = false; % disable % Retrieve some common properties mapValue = map.Value; xAxisIndex = xAxis.Index; xAxisValue = xAxis.Value; yAxisIndex = yAxis.Index; yAxisValue = yAxis.Value; % Perform basic checks if numel(xAxisValue)<2 error(message('mbc:calibrationdata:dcminterface:InvalidOperation', numel( xAxisValue ))); elseif numel(yAxisValue)<2 error(message('mbc:calibrationdata:dcminterface:InvalidOperation', numel( yAxisValue ))); elseif isempty(mapValue) error(message('mbc:calibrationdata:dcminterface:InvalidOperation4')); end % Are axes simple? (i.e. no skipped breakpoints) xAxisIsSimple = all(xAxisIndex==1:numel(xAxisIndex)) && numel(xAxisValue)==size(mapValue,2); yAxisIsSimple = all(yAxisIndex==1:numel(yAxisIndex)) && numel(yAxisValue)==size(mapValue,1); % Header if xAxis.Monospaced && yAxis.Monospaced keyword = 'FESTKENNFELD'; else keyword = 'KENNFELD'; end header = sprintf('%s %s %d %d\n', keyword, map.Identifier, size(map.Value,2), size(map.Value,1)); if xAxisIsPublic && xAxisIsSimple header = sprintf('%s*SSTX %s\n', header, xAxis.Identifier); end if yAxisIsPublic && yAxisIsSimple header = sprintf('%s*SSTY %s\n', header, yAxis.Identifier); end % Metadata metadata = sprintf(' LANGNAME "%s"\n EINHEIT_X "%s"\n EINHEIT_Y "%s"\n EINHEIT_W "%s"\n', ... map.Description, xAxis.Units, yAxis.Units, map.Units); % Compute breakpoints, interpolating when necessary if ~xAxisIsSimple xAxisValue = interp1(xAxisIndex, xAxisValue, 1:size(mapValue,2)); end if ~yAxisIsSimple yAxisValue = interp1(yAxisIndex, yAxisValue, 1:size(mapValue,1)); end % Data body = cell(1,1+length(yAxisValue)); % preallocate % X axis thisXAxis = iRow2Str(xAxisValue, 'ST/X'); body{1} = thisXAxis; for yc = 1:length(yAxisValue), % loop over y values % This Y thisYAxis = iRow2Str(yAxisValue(yc), 'ST/Y'); % Value thisValue = iRow2Str(mapValue(yc,:), 'WERT'); % Concatenate body{yc+1} = [thisYAxis thisValue]; end % Footer footer = sprintf('%s\n', 'END'); % Combine text = [header metadata [body{:}] footer]; % ------------------------------------------------------------------------- function str = iRow2Str(row, identifier) %IROW2STR Helper to convert a numeric row vector and identifier into a %DCM-style string. str = ''; % initialize % Loop through elements of row, nvals at a time nvals = 6; % values per row for sc = 1:ceil(length(row)/nvals), % Careful about start and end positions thisStart = 1+nvals*(sc-1); thisEnd = min(nvals*sc, length(row)); % Get chunk of row thisValue = row(thisStart:thisEnd); % Write identifier thisLine = sprintf(' %s', identifier); % Write values for vc = 1:length(thisValue), thisLine = sprintf('%s %s', thisLine, num2str(thisValue(vc),10)); end % Append thisLine = sprintf('%s\n', thisLine); str = [str thisLine]; end