Can one save the filename as a variable in 'readtable'?

4 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 11 月 28 日
コメント済み: alpedhuez 2020 年 11 月 29 日
Suppose I use
T=readtable('texas,csv')
I want to save the name of the file (texas) as a variable. Please advise.
  1 件のコメント
alpedhuez
alpedhuez 2020 年 11 月 29 日
Let me work on it.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 11 月 29 日
See the FAQ:
Adapting it would go something like this:
% Specify the folder where the files live.
myFolder = pwd; % or '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, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in with readtable() or csvread().
data{k} = readtable(fullFileName);
end
fprintf('Done processing %d CSV files.\n', length(theFiles));
Results are in a cell array.
It needs to be a cell array because the data in the files might not all be the same size.
  1 件のコメント
alpedhuez
alpedhuez 2020 年 11 月 29 日
Let me work on it.

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 28 日
編集済み: Ameer Hamza 2020 年 11 月 28 日
you can save char arrays in a variable and then concatenate it with the extension using [] operator.
filename = 'texas';
T = readtable([T '.csv'])
  5 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 11 月 29 日
Are you trying to name the variables named like texas, iowa, etc. For example
filename = 'texas';
texas = readtable([T '.csv'])
alpedhuez
alpedhuez 2020 年 11 月 29 日
Let me work on it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by