Why can't I open a file on MacBook?

조회 수: 21 (최근 30일)
Marcin Sajdak
Marcin Sajdak 2021년 11월 17일
댓글: Marcin Sajdak 2021년 11월 17일
Hi!
I've tried to run this code and open a file on macbook but here's still this an error.
When I used this code on Windows, it used to work.
Does anyone knows why?
path = '/Users/martine/Desktop/projekt/csv/Stop Signal'
folder = dir('*.csv')
x = folder(1).name
Error is:
Index exceeds the number of array elements (0).

채택된 답변

Steven Lord
Steven Lord 2021년 11월 17일
I recommend not creating a variable named path, as path already has a meaning in MATLAB. See doc path for more information on that function.
Just because you define a variable named path does not mean that functions like dir will automatically look there. I recommend you use fullfile to assemble the path to the folder and the extension you want to search for.
location = fullfile(matlabroot, 'toolbox', 'matlab', 'general')
location = '/MATLAB/toolbox/matlab/general'
filespec = 'bench*';
D = dir(fullfile(location, filespec))
D = 2×1 struct array with fields:
name folder date bytes isdir datenum
{D.name}.'
ans = 2×1 cell array
{'bench.dat'} {'bench.m' }
If I'd just asked for the file using the filespec it wouldn't have looked in that directory and so wouldn't have found any such files.
D2 = dir(filespec)
D2 = 0×1 empty struct array with fields: name folder date bytes isdir datenum

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by