How to read a txt file with comma, text and numbers in matlab, and export the data
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
chihyu
am 27 Okt. 2021
Kommentiert: chihyu
am 28 Okt. 2021
The txt file I exported from the analysis software contains commas and text and numbers (as shown in the figure below). This prevents me from directly using load data
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/780363/image.png)
The above txt file can be imported into excel using [Data Analysis Wizard] to split it into multiple cells, but I have thousands of these files, I can’t extract data from excel one by one, I want to use more automated Way to extract.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/780368/image.png)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Okt. 2021
See also readtable()
3 Kommentare
Walter Roberson
am 28 Okt. 2021
clc;
clear all;
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '**/*.txt'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
num_files = length(theFiles);
t = cell(num_files,1);
for k = 1 : num_files
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
t{k} = readtable(fullFileName);
end
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!