How can I use an external driver to perform an analysis in Matlab?

2 ビュー (過去 30 日間)
Yasmin
Yasmin 2022 年 10 月 24 日
回答済み: Maneet Kaur Bagga 2024 年 2 月 15 日
Hello,
I have a lot of data on a external hard drive. I am currently trying to run an analysis, which works when I analyze data from my "normal" data storage. I normally use this directory:
guiPromptSpectroPlotCsi C:\Users\myname\Spectroscopy
However when trying to access the external hard driver with:
guiPromptSpectroPlotCsi J:\Spectroscopy raw data
I get the following notification
Error using guiPromptSpectroPlotCsi
Requested directory does not exist.
It also doesn't work when I use the name of the external hard driver which is:
K>> guiPromptSpectroPlotCsi number\Spectroscopy raw data
Thank you for your help!

回答 (1 件)

Maneet Kaur Bagga
Maneet Kaur Bagga 2024 年 2 月 15 日
Hi,
As per my understanding to access the directory on the external hard drive you can do it using the "dir" command. Please ensure that you have the correct drive letter for your external hard drive and refer to the code below as a possible workaround.
clear all
TopFolder = 'J:\'; % Replace 'J' with the correct drive letter of your external hard drive
SubFolder = dir(TopFolder);
SubFolder = SubFolder(3:end); % the first two here are just pointers
a = struct2cell(SubFolder);
Name = a(1,:);
% This will give you the names of the folders where your data is located.
% Now, let's get the names of each of the files in those folders:
b = cellfun(@(x) dir(fullfile(TopFolder, x)), Name, 'UniformOutput', false);
c = cellfun(@(x) x(3:end), b, 'UniformOutput', false); % Exclude '.' and '..' from the file list
d = cellfun(@(x) struct2cell(x), c, 'UniformOutput', false);
FileS = cellfun(@(x) x(1,:), d, 'UniformOutput', false);
% FileS now contains the names of all the files in each subfolder under your top folder.
% You can access individual files using FileS{folderIndex}{fileIndex}.
% For example, to access the first file in the first subfolder, you would use:
firstFile = fullfile(TopFolder, Name{1}, FileS{1}{1});
% Now you can import the data using one of the built-in MATLAB functions like importdata or textscan:
% (Assuming the files are text files and you want to use textscan)
fileID = fopen(firstFile, 'r'); % Open the file for reading
% You may need to adjust the formatSpec to match the format of your data.
formatSpec = '%f'; % Example formatSpec for floating-point numbers
data = textscan(fileID, formatSpec);
fclose(fileID); % Always close the file when you're done
% 'data' now contains the contents of the first file in the first subfolder.
Please refer to the following MATLAB Answers for a similar workaround:
  1. https://www.mathworks.com/matlabcentral/answers/35249-retrieve-data-from-hard-drive-local-directory
  2. https://www.mathworks.com/matlabcentral/answers/111157-access-file-from-ext-hdd
Please refer to the following MATLAB Documentation to import the data:
Hope this helps!

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by