Azzera filtri
Azzera filtri

Removing the line number from a numerical matrix

1 visualizzazione (ultimi 30 giorni)
I was using the below code a few months ago to separate a file into smaller matrices, while removing the line number on each row. Now that I have gone back to use it again, It has stopped removing the line number. I'm not sure what could have occurred in the intermediate time to cause the code to stop preforming as it had been. Any ideas?
myFile = uigetdir('C:\Users\c13459232\Documents\MATLAB'); % Generate command window to choose a folder
if ~isdir(myFile) % if the directory is not a valid path
errorMessage = sprintf('Error: the following file does not exist: \n%s', myFile); % print this error message
uiwait(warndlg(errorMessage)); % block the execution of program and wait to resume
f2d = fopen(fullfile(myFile,'temp_01.asc'),'w'); % open a file to have data written into (fullfile builds full file name from parts)
f1d = fopen(fullfile(myFile,'TEST_A.asc'),'r'); % open the file to me looked at ('r' means read file)
k = 0;
while ~feof(f1d) % while find end of file, the TEST_A file
str = fgetl(f1d); %fgetl reads TEST_A line by line and this is identified by str
[row, ~, ~, idx] = sscanf(str, '%d'); % Read formatted data from string ignoring the first element of each row
if sscanf(str, '%d') ==1 % if the data (read line by line) from a string is equal to one
k = k+1; % k should increase by one with every block
fclose(f2d); % close the file that is being written to
fnm = fullfile(myFile,sprintf('temp_%02d.asc',k)); %build a full file from parts that are being formatted into strings
f2d = fopen(fnm,'w'); %open the parts to be written
fprintf(f2d,'%s\n',str); % writes the blocks to the file
fclose(f1d); %close the original TEST_A file
fclose(f2d); %close the new file
message = sprintf( 'Task Complete' );
  7 Commenti
dpb il 12 Mag 2017
I went and looked at the previous thread back in February where you had code supplied to build these arrays.
Why don't you fix that code to correct the size issue there instead of propagating it downstream?
Stephen23 il 17 Mag 2017
Modificato: Stephen23 il 17 Mag 2017
"Surely much betters ways to do that than to process line-by-line in text mode"
You are obviously much better than I am if you can read 50x1024x1025 values into memory without any "out of memory" error. The data, which was eventually delivered to me via dropbox, is large. My code, which was developed after attempting many different solutions, including reading the data into numeric arrays, is clearly that of an incompetent code monkey. Yep, I certainly look forward to seeing your "much betters ways" (you use the plural, so please provide multiple much better ways of copying a large amount of text data from one text file to some others).

Accedi per commentare.

Risposta accettata

Stephen23 il 17 Mag 2017
Modificato: Stephen23 il 17 Mag 2017
The answer to your question is that you need to use the code that I gave you, and not remove that parts that removes the line number, in particular this line:
fprintf(f2d,'%s\n',str(idx+1:end)); % this is the correct line.
This was clearly shown in my comment to your original request for this feature:
In that comment I also explained how it works, and showed a small working example. Simply clicking the idx variable would have shown you that it was unused in your code (the editor shows where variables are used).

Più risposte (1)

dpb il 12 Mag 2017
Modificato: dpb il 17 Mag 2017
The obvious answer would be this isn't the same code as you ran previously. There's nothing in this code that would do anything except echo what was read to the new file...
str = fgetl(f1d);
...[some code that doesn't do anything to _str_ elided for clarity]...
fprintf(f2d,'%s\n',str); % writes the block
That's the guts of the code you have here--it reads a line and then writes it to a second file verbatim. Whatever processing you did to remove something from the record before rewriting it is missing.
Sans the user interface stuff, the following should work quite a lot quicker...
data=dlmread(fullfile(myFile,inFileName),';'); % read the input file
data=data(:,2:end); % eliminate first column
nCol=size(data,2); % column size
data=reshape(data.',nCol,nCol,[]); % reshape to 3D to write planes
nBlk=size(data,3); % find out how many there are
for k=1:nBlk % and loop over them
dlmwrite(fnm,data(:,:,k),';'); % and write it out
"... dlmread results in an out of memory error"
OK, can't read whole file at once...the initial code of just a fgetl loop is simple; don't see what the issue must be that's so baffling, but an alternative that might be a little quicker still processing only a single block at a time...
N=50; % block size, records
k=0; % block counter
fid=fopen(fullfile(myFile,inFileName),'r'); % input file
while ~feof(fid)
k=k+1; % increment block counter
fnm=fullfile(myFile,sprintf('temp_%02d.asc',k)); % output file name
data=textscan(fid,'',N,'delimiter',';'); % read block
dlmwrite(fnm,cell2mat(data(2:end)),';'); % and write it out less first cell content (line numbers)
I still wonder why don't do this initially, though, when created these files instead of needing second pass...
  6 Commenti
Aaron Smith
Aaron Smith il 17 Mag 2017
The code you had written before was not ignoring the line number. I was using it intact
Stephen23 il 17 Mag 2017
Modificato: Stephen23 il 17 Mag 2017
@Aaron Smith: "I was using it intact" The code in your question above uses this line:
fprintf(f2d,'%s\n',str); %
and the code that I gave you in February uses this line:
You can find my original code (including the code that removes the line number) here:

Accedi per commentare.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by