📄 loadxsil.m
字号:
disp('using double precision data') end evalStr = ''; evalStr = sprintf('%s%s\n',evalStr,'% open the binary data file for reading'); evalStr = sprintf('%s%s%s%s%s%s\n',evalStr,'fpDat = fopen(''',datFname,''',''r'',''',machineFormat,''');'); evalStr = sprintf('%s%s\n', evalStr, 'if (fpDat < 0)'); evalStr = sprintf('%s%s%s%s\n', evalStr, ' fprintf(''Cannot open binary data file: ',datFname,''')'); evalStr = sprintf('%s%s\n', evalStr, ' return'); evalStr = sprintf('%s%s\n', evalStr, 'end'); % now the time to be a little tricky for i = 1:numIndepVars indexStrArray(i) = cellstr(strcat('index',num2str(i))); end % now try and load the variables into memory % I'm automatically generating the string that needs to be evaluated by matlab % and then running eval on it. % first, create a string of the variables to be loaded (this saves shitloads % of time). subscriptArray = {}; varsStr = ''; indepVarsStr = ''; if (debug) fprintf('num independent vars = %d\n',numIndepVars); fprintf('num variables in total = %d\n',numVars); end for k = 1:numVars if (debug) fprintf('varNames(%d).name = %s\n',k,varNames(k).name); end if (k <= numIndepVars) indepVarsStr = sprintf('%s%s%s%s\n',indepVarsStr,varNames(k).name,'Len',' = fread(fpDat,1,''ulong'');'); indepVarsStr = sprintf('%s%s%s%s%s\n',indepVarsStr,varNames(k).name,' = fread(fpDat,',varNames(k).name,'Len,precision);'); elseif (k > numIndepVars) varsStr = sprintf('%s%s%s%s\n',varsStr,varNames(k).name,'Len',' = fread(fpDat,1,''ulong'');'); if (numIndepVars == 1) varsStr = sprintf('%s%s%s%s%s\n',varsStr,varNames(k).name,' = fread(fpDat,',varNames(1).name,'Len,precision);'); elseif (numIndepVars == 2) varsStr = sprintf('%s%s%s%s%s%s%s\n',varsStr,varNames(k).name,' = fread(fpDat,[',varNames(2).name,'Len,',varNames(1).name,'Len],precision);'); elseif (numIndepVars > 2) % now we need to create a multi-dimensional matrix, and this is harder to do... % we need to read in a matrix-sized (ie 2D) block at a time, and append this to the other dimensions % the number of independent variables determines the dimensions of the N-D matrix to produce % construct the for loop to loop over the third and subsequent dimensions for inumIndepVars = 1:numIndepVars-2 varsStr = sprintf('%s%s%s%s%s%s\n',varsStr,'for ',char(indexStrArray(inumIndepVars)),' = 1:',varNames(inumIndepVars).name,'Len'); end % generate the first part of the string, which is the array to be assigned into varsStr = sprintf('%s%s%s',varsStr,varNames(k).name,'(:,:,'); for inumIndepVars = 1:numIndepVars-2 varsStr = sprintf('%s%s',varsStr,char(indexStrArray(inumIndepVars))); % need to append a comma if not last index to append if (inumIndepVars ~= numIndepVars-2) varsStr = sprintf('%s%s',varsStr,','); end end % generate the fread statement varsStr = sprintf('%s%s%s%s%s%s\n',varsStr,') = fread(fpDat,[',varNames(numIndepVars).name,'Len,',varNames(numIndepVars-1).name,'Len],precision);'); % finish off the for loop for inumIndepVars = 1:numIndepVars-2 varsStr = sprintf('%s%s\n',varsStr,'end'); end% disp('Error:')% disp('The number of independent variables is greater than 2')% disp('Unfortunately, loadxsil cannot yet handle this situation with binary data')% disp('Please use ascii data and the xsil2graphics utility program bundled with xmds') %return; % %%% the following code is for reference only, not to be used!!!% varsStr = sprintf('%s%s%s',varsStr,varNames(k).name,' = fread(fpDat,[');% for l = numIndepVars:-1:2 % we need to go backwards% varsStr = sprintf('%s%s%s',varsStr,varNames(l).name,'Len,');% end% % now add the line that doesn't have the sepatating comma at the end% varsStr = sprintf('%s%s%s',varsStr,varNames(1).name,'Len');% % and complete the string% varsStr = sprintf('%s%s\n',varsStr,'],precision);');% %%% end of reference code end end end % now generate the string to evaluate evalStr = sprintf('%s%s',evalStr,indepVarsStr); evalStr = sprintf('%s%s',evalStr,varsStr); % remember to close the binary file evalStr = sprintf('%s%s\n',evalStr, 'fclose(fpDat);'); if (debug) disp(' ') disp('This is the string to be evaluated:') disp(' ') disp(evalStr) return end % and evaluate it disp(' ') disp('This is the string to be evaluated:') disp(' ') disp(evalStr) eval(evalStr) % assign output variables for i = 1:numVars assignin('caller',varNames(i).name,eval(varNames(i).name)); end % clean up and return if (imoments == numMomentGroups) fclose(fp); disp('done!') return end else % well, it's text, so now call the text loading routine disp('loading ascii data (this may take a while)') % now the time to be a little tricky for i = 1:numIndepVars indexStrArray(i) = cellstr(strcat('index',num2str(i))); end % now try and load the variables into memory % I'm automatically generating the string that needs to be evaluated by matlab % and then running eval on it. % first, create a string of the variables to be loaded (this saves shitloads % of time). subscriptArray = {}; % make the fscanf %e bit at the front of the string varsStr = 'A = fscanf(fp,'''; for k = 1:numVars varsStr = sprintf('%s%s',varsStr,'%e '); end varsStr = sprintf('%s%s%s%s\n',varsStr,''',',num2str(numVars),');'); for k = 1:numVars if (k <= numIndepVars) indexStr = strcat('(',char(indexStrArray(k)),')'); subscriptArray(k) = cellstr(indexStr); elseif (k > numIndepVars) indexStr = strcat('(',char(indexStrArray(1))); for j = 2:numIndepVars indexStr = strcat(indexStr,',',char(indexStrArray(j))); end indexStr = strcat(indexStr,')'); subscriptArray(k) = cellstr(indexStr); end varsStr = sprintf('%s%s%s%s%s%s\n',varsStr,varNames(k).name,char(subscriptArray(k)),' = A(',num2str(k),');'); end % now generate the string for preallocation preStr = ''; for k = 1:numVars if (k <= numIndepVars) indexStr = strcat('(',num2str(loopInd(k)),',1)'); subscriptArray(k) = cellstr(indexStr); elseif (k > numIndepVars) indexStr = strcat('(',num2str(loopInd(1))); for j = 2:numIndepVars indexStr = strcat(indexStr,',',num2str(loopInd(j))); end indexStr = strcat(indexStr,')'); subscriptArray(k) = cellstr(indexStr); end preStr = sprintf('%s%s%s%s%s\n',preStr,varNames(k).name,' = zeros',char(subscriptArray(k)),';'); end % now generate the string to evaluate evalStr = ''; for i = 1:numIndepVars evalStr = sprintf('%s%s%s%s%s\n',evalStr,'for ',char(indexStrArray(i)),' = 1:',num2str(loopInd(i))); if (i == 1) evalStr = sprintf('%s%s%s%s%s%s\n',evalStr,'if (rem(',char(indexStrArray(i)),',',num2str(loopInd(i)/10),') < 1)'); evalStr = sprintf('%s%s%s%s%s%s\n',evalStr,'fprintf(''%3.0f%% completed\n'',',char(indexStrArray(i)),'*100/',num2str(loopInd(i)),')'); evalStr = sprintf('%s%s\n',evalStr,'end'); end end evalStr = sprintf('%s%s',evalStr,varsStr); for i = 1:numIndepVars evalStr = sprintf('%s%s\n',evalStr,'end'); end if (debug) disp(evalStr); else % and evaluate it eval(evalStr); end % assign output variables for i = 1:numVars assignin('caller',varNames(i).name,eval(varNames(i).name)); end % clean up and return if (imoments == numMomentGroups) fclose(fp); disp('done!') return end end endendfclose(fp);return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -