What is the format to write to a file not in the MATLAB directory.

조회 수: 6 (최근 30일)
Edward_A
Edward_A 2018년 12월 30일
댓글: Image Analyst 2019년 1월 5일
I have macOS 12.14.2 Mohave using MATLAB Home.
What is the format to write to a file not in the MATLAB directory. I have a KINGSTON flash drive and I use
c=[1.1 2.2 3.3 4.4 5.5];
e=[15.5 16.6 17.7 18.8 19.9];
fid = fopen('\KINGSTON\test1.txt','w')
fprintf(fid,'%6.2f %8.4f\n',c,e);
fclose(fid)
The file is not created on the KINGSTON drive. I get fid=3.
If I use fid = fopen('test1.txt','w') the file is created in the current folder. What am I doing wrong?

채택된 답변

Ken Atwell
Ken Atwell 2019년 1월 3일
This is a great question. I don't have a flash drive handy to try this, but you might need to mount the flash drive to make it accessible from the file system and MATLAB. Here is a web page on how to do this:
That might be enough. However, there is also the further possibility that, to make this mount visibile to MATLAB, you might also need to launch MATLAB from the Terminal window created above. The command to do this from Terminal will be something like this:
open -a "/Applications/MATLAB R2018b.app/"
Hope this helps.
If all of this is too much hassle, the poor man's solution is to write your files to some place that is more readily accessible, and then later manaully move/copy those files to the flash drive using Finder.

추가 답변 (4개)

Image Analyst
Image Analyst 2018년 12월 30일
편집: Image Analyst 2018년 12월 30일
I would use 'wt' and fullfile(). Try this, changing the drive letter to what it really is.
folder = 'E:\KINGSTON'; % Windows uses a drive letter.
fullFileName = fullfile(folder, 'test1.txt');
fid = fopen(fullFileName, 'wt')
fprintf(fid,'%6.2f %8.4f\n', c, e);
fclose(fid);
fprintf('Done!\n');
if exist(fullFileName, 'file')
message = sprintf('%s successfully created.\n', fullFileName);
uiwait(helpdlg(message));
else
message = sprintf('FAILED\n%s\nNOT created.\n', fullFileName);
uiwait(warndlg(message));
end
By the way, did you look for a KINGSTON subfolder of your current folder rather than on the flash drive? It might have made it there.
  댓글 수: 2
Edward_A
Edward_A 2019년 1월 3일
편집: Walter Roberson 2019년 1월 4일
This did not answer my question. There is no E: drive on a MAC.
Here is what I have:
c=[1.1 2.2 3.3 4.4 5.5];
e=[15.5 16.6 17.7 18.8 19.9];
fid = fopen('test1.txt','w')
fprintf(fid,'%6.2f %8.4f\n',c,e);
fclose(fid)
Creates the file in the current folder MATLAB which is equivalent to
/Users/edwardandruthnelson/Documents/MATLAB/test1.txt
When I use
fid = fopen('/Users/edwardandruthnelson/Documents/MATLAB/test1.txt','w');
instead
the file is created in the same folder.
When I use
fid = fopen('/Users/edwardandruthnelson/Documents/test1.txt','w');
the file is created in the folder /Users/edwardandruthnelson/Documents
I have a KINGSTON flash drive. When I try
fid = fopen('/KINGSTON/test1.txt','w');
or
fid = fopen('/Edward’s iMac/KINGSTON/test1.txt','w');
the file is not created. I get fid = -1. Why?
Steven Lord
Steven Lord 2019년 1월 3일
Call fopen with two outputs to try to write the file on the flash drive. If fid is -1 what does the second output contain?

댓글을 달려면 로그인하십시오.


Edward_A
Edward_A 2019년 1월 3일
I'm not sure how to do it. Please send me the code and I will try it. Thanks.
Ed
PS Is there a way to add the drive with addpath? I'm getting ther, but too sowly.

Edward_A
Edward_A 2019년 1월 4일
Hi Ken,
The flash drive is visible on the left side when I double click on finder. In earlier versions, using windows, one could do this using a: or c:. I think you are correct that the easy way is to copy the file and paste into my flash drive.
I now feel comfortable to call Mathworks to see if there is something that isn't a hassle.
Thanks for your input. You have been very helpful.
Ed Nelson

Walter Roberson
Walter Roberson 2019년 1월 4일
편집: Walter Roberson 2019년 1월 4일
Flash drives are not mounted at the root of the file system. You need to refer to /Volumes/KINGSTON/test1.txt
To check the mount point, at the MATLAB prompt (on Mac)
!df
and look on the right hand side of the output to see the "mounted on" information.
Note: MacOS and OS-X mount under /Volumes . Linux tends to mount under /media
  댓글 수: 1
Image Analyst
Image Analyst 2019년 1월 5일
Edward's Answer moved here:
Thank you Walter. The code:
c=[1.1 2.2 3.3 4.4 5.5];
e=[15.5 16.6 17.7 18.8 19.9];
fid = fopen('/Volumes/KINGSTON/test1.txt','w');
fprintf(fid,'%6.2f %8.4f\n',c,e);
fclose(fid);
worked with no problem and the file was saved on the flash drive.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by