I read a .txt file into a 1x1 cell array using textscan. How to I extract certain data from that 1x1 cell array?
The .txt file has mixed format data.
The code I have so far:
%% Import Safir output file
FileName="PROOV_6.txt";
tic
%% Read the txt file
FID = fopen(FileName, 'r');
if FID == -1
error('Cannot open file')
end
Data = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', ' ' );
CStr = Data{1};
fclose(FID);
%% Find all row numbers that contain the string
Index = strfind(CStr, 'TOTAL TEMPERATURES');
IndexA = find(not(cellfun('isempty', Index)));
%% Loop through CStr accessing matrices between strings "TOTAL TEMPERATURES"
%% ----------
%% Save the file again
FID = fopen(FileName, 'w');
if FID == -1
error('Cannot open file')
end
fprintf(FID, '%s\n', CStr{:});
fclose(FID);
toc
I will also provide a link to the .txt file:
PROOV_6.txt
Use {} indexing to get a value out of a cell array. That there is only one cell makes no difference. mycell{1}.
Related
Matlab help: I am trying to export all 50 points to a CSV. How can I append the csv files after each iteration?
%import csv file
filename = 'Q:\Electroporation\raw_works.csv';
delimiter = ',';
startRow = 2;
%% Format for each line of text:
formatSpec = '%s%f%f%f%f%f%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Create output variable
rawworks = table(dataArray{1:end-1}, 'VariableNames', {'Name','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'});
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
So far the data in MATLAB.
% store data into a variable
table= rawworks;
array=str2double(table2array(table)); % convert to array
ss= size(array)
N= ss(1)
ones=ones(50,14);
xls=zeros(50,14);
Now I do the math operation
for i= 1:N
A=[array(i,2) array(i,3) array(i,4);array(i,5) array(i,6) array(i,7);array(i,8) array(i,9) array(i,10)];
%diognalize
[U,S,V]=svd(A);
P1=S(1,1);
P2=S(2,2);
P3=S(3,3);
%output data
data_for_excel_file=[A(1,1) A(1,2) A(1,3) A(2,1) A(2,2) A(2,3) A(3,1) A(3,2) A(3,3) P1 P2 P3 P1/P2 P1/P3 ]
here is where I'm having problems. How can I make csvwrite append to the end of % file. Currently, it is only writing out the last result instead of all 50.
csvwrite('Diognalized_output.csv',data_for_excel_file,1) %HELP
end
If you are okay using the more general function dlmwrite instead, you can use its -append flag to add your output to the end of the file each time.
Change your last line from
csvwrite('Diognalized_output.csv',data_for_excel_file,1)
to
dlmwrite('Diognalized_output.csv,data_for_excel_file,'-append')
The default delimiter for dlmwrite is the comma (,) so you get the same output format here.
I try to skip lines 5 to the end of the file from a .txt-file I import into Matlab.
fidM = fopen('abc.txt', 'r');
for i = 5:150
fgetl(fidM);
end
buffer = fread(fidM, Inf) ;
fclose(fidM);
fidM = fopen('xyz.txt', 'w');
fwrite(fidM, buffer) ;
fclose(fidM) ;
The code above does not do the job somehow. Any ideas?
Your code currently reads the first 146 lines of your file, discards them, then reads the remainder and writes that out to a file. Instead, if you want to just write the first 5 lines of abc.txt into xyz.txt, then do something like the following:
fid = fopen('abc.txt', 'r');
fout = fopen('xyz.txt', 'w');
for k = 1:5
fprintf(fout, '%s\r\n', fgetl(fid));
end
fclose(fid);
fclose(fout);
Or you can remove the loop and do something like this:
fid = fopen('abc.txt', 'r');
% Read in the first 5 lines
contents = textscan(fid, '%s', 5);
fclose(fid);
% Write these to a new file
fout = fopen('xyz.txt', 'w');
fprintf(fout, '%s\r\n', contents{1}{:});
fclose(fout);
I want to import Multiple Files (waveforms in .wf format) using the Import Tool in MATLAB 2016a.
This is my (auto-generated) function
function Ring607062016175832 = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as a matrix.
% RING607062016175832 = IMPORTFILE(FILENAME) Reads data from text file
% FILENAME for the default selection.
%
% RING607062016175832 = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data
% from rows STARTROW through ENDROW of text file FILENAME.
%
% Example:
% Ring607062016175832 = importfile('Ring6_07-06-2016_17-58-32.wf', 1, 50001);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2016/10/12 13:02:36
%% Initialize variables.
delimiter = ';';
if nargin<=2
startRow = 1;
endRow = inf;
end
%% Format string for each line of text:
% column1: double (%f)
% column2: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
%% Close the text file.
fclose(fileID);
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Create output variable
Ring607062016175832 = table(dataArray{1:end-1}, 'VariableNames', {'VarName1','VarName2'});
When I run the function with input arguments such as
importfile('Ring6_07-06-2016_17-58-32.wf', 1, 50001)
I get the valid result for one imported file.
My problem:
I want to create a "for" loop to import the data from all ".wf" files.
Using this example I created this "for" loop in a separate script:
numFiles = 361;
startRow = 1;
endRow = 50001;
WaveformData = cell(1,numFiles);
for fileNum = 1:numFiles
filename = sprintf('Ring6%02d.wf',fileNum);
WaveformData{fileNum} = importfile('filename', 1, 50001);
end
But when I try to run it, I get this error:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in importfile (line 36)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue'
,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
Error in Schleife (line 8)
WaveformData{fileNum} = importfile(filename, startRow, endRow);
Can anybody tell me what I did wrong or give me a hint how to create a working "for"-loop to import all the files?
Change
WaveformData{fileNum} = importfile('filename', 1, 50001);
to
WaveformData{fileNum} = importfile(filename, 1, 50001);
without the ' '
I'm using Matlab's import data code generator to pass data to a series of commands. This works fine when I run the script and reference a single file, but if I loop through several files, my variables aren't updated as I expect. I believe I have traced the problem to 'fileID' not updating after the first iteration of the loop.
In the code below, I can confirm that 'filename' is updated with each iteration of the loop, while 'fileID' is not. Consequently, the same vector is assigned to the variable 'y' in each iteration.
Can anyone suggest where I am going wrong?
FileList = dir('*.csv');
N = size(FileList,1);
for k = 1:N
% get the file name:
filename = FileList(k).name;
delimiter = ',';
startRow = 2;
%% Format string for each line of text:
% column2: double (%f)
% column3: double (%f)
% column4: double (%f)
% column5: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%*s%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names
O1 = dataArray{:, 1};
H1 = dataArray{:, 2};
L1 = dataArray{:, 3};
C1 = dataArray{:, 4};
%% Test filename and fileID
filename
fileID
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
y=C1;
figure
plot(y);
end
FileID is not supposed to change like you expect. fileID is only a file identifier, the extracted data is in dataArray as you scan the text with the help of fileID.
So that FileID will be usually equal to 3 if you just open the file and you closed it before open a new one. If you don't close it there will be a different number in fileID for each file.
I am trying to read a .csv file in matlab and saving it as .txt. but it seems that when i print the lines in my .txt file, the line is nothing:
fid1 = fopen('input.csv');
fid = fopen('output.txt', 'w');
tline = fgetl(fid1);
tline = strrep(tline,'remove','')
fprintf(fid, '%s\n', tline);
fclose(fid1);
fclose(fid);
when i open the file afterwards the file is empty
if i convert the .csv file to .txt using excell there is no problem.
how do i easily convert the .csv data into .txt?
i can't do it manually because i have hundreds of .csv files
I think you are not processing every line of your csv file (if the first line is empty it explains your empty txt files).
fid1 = fopen('test.csv');
fid = fopen('output.txt', 'w');
tline = fgetl(fid1);
while ischar(tline)
tline = strrep(tline,'remove','');
fprintf(fid, '%s\n', tline);
tline = fgetl(fid1);
end
fclose(fid1);
fclose(fid);