My variable doesn't show up in my workspace and then Matlab stalls?
Show older comments
I'm trying to read a relatively large csv (15,0000 x 2000) table into MATLAB. I've functionalized a loop to read the code using textscan:
% Opens the files using the fID.
fID = fopen(filename);
% Uses textscan to read in the file using the carriage return as a delimiter.
Lines = textscan(fID,'%s','delimiter', {'\n','\r'});
Lines = Lines{1,1};
% Determines the number of lines and ignores the first line, which is a single
% string.
linesize = size(Lines,1)-1;
% Uses a comma/tab delimiter to separate first line
Line = Lines{2,1};
Row = textscan(Line,'%s','delimiter',',\t');
Row = Row{1,1};
dataout = Row';
% Separates subsequent lines
for ind = 3:linesize
Row = textscan(Lines{ind,1},'%s','delimiter',',\t');
Row=Row{1,1};
dataout(ind-1,:)=Row';
end
When my function returns to the commandline, the output variables from the function do not appear in the Workspace terminal. When I call "Who", they are listed there. When I try to open a variable (any variable) in the command line, the program sits "busy" for several minutes until I get fed up with waiting for Matlab to print the variable output and terminate the code.
I know my problem isn't associated with using the avaliable memory, as I've tried this before on other occasions and it either generates an error report or it prints back to the command line that memory has been exceeded.
I'm wondering if anyone has had a similar problem, and how it was handled.
Thanks!
3 Comments
What exactly is the problem? The function runs fine, but the display of the results in the command window takes longer than you want? But why do you want to display 30'000'000 elements in the command window or workspace browser? Or are there any problems with the function?
Justine
on 7 Mar 2013
Sean de Wolski
on 7 Mar 2013
If you close and reopen the workspace does it show up?
Answers (1)
It was not your problem, but I guess this is faster:
fID = fopen(filename);
Line1 = fgetl(fID);
Row = textscan(Line1, '%s', 'delimiter', ',\t');
nCol = numel(Row{1});
fseek(fID, 0, -1);
data = fscanf(fID, '%g, ', [nCol, inf]);
fclose(fID);
Or perhaps this, when there is no trailing comma in each line:
fmt = repmat('%g, ', 1, nCol);
fmt(end-1:end) = [];
data = fscanf(fID, fmt, [nCol, inf]);
And finally transpose the data on demand.
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!