Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>"GOLF" ANSWER:</strong></p> <p>This starts with the answer from <a href="https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1540031#1540031">mtrw</a> and shortens it further:</p> <pre><code>fid = fopen('testMixed.txt','rt'); data = textscan(fid,'%s','Delimiter','\n','MultipleDelimsAsOne','1',... 'CommentStyle','%'); fclose(fid); data = strcat(data{1},{' '}); data = sscanf([data{:}],'%i',[sum(isspace(data{1})) inf]).'; </code></pre> <p><strong>PREVIOUS ANSWER:</strong></p> <p>My first thought was to use <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html" rel="nofollow noreferrer">TEXTSCAN</a>, since it has an option that allows you to ignore certain lines as comments when they start with a given character (like <code>%</code>). However, TEXTSCAN doesn't appear to handle numbers in hexadecimal format well. Here's another option:</p> <pre><code>fid = fopen('testMixed.txt','r'); % Open file % First, read all the comment lines (lines that start with '%'): comments = {}; position = 0; nextLine = fgetl(fid); % Read the first line while strcmp(nextLine(1),'%') comments = [comments; {nextLine}]; % Collect the comments position = ftell(fid); % Get the file pointer position nextLine = fgetl(fid); % Read the next line end fseek(fid,position,-1); % Rewind to beginning of last line read % Read numerical data: nCol = sum(isspace(nextLine))+1; % Get the number of columns data = fscanf(fid,'%i',[nCol inf]).'; % Note '%i' works for all integer formats fclose(fid); % Close file </code></pre> <p>This will work for an arbitrary number of comments at the beginning of the file. The computation to get the number of columns was inspired by <a href="https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1539410#1539410">Jacob's answer</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload