Why am I unable to open file with dlmwrite?
4 次查看(过去 30 天)
显示 更早的评论
I'm using ssh to get into a computer cluster and running a Matlab program on it.
I'm trying to take in a read only text file from a read only directory, and output a text file into my own directory.
I also want the outputted data text file to be the name of my input, so that is why I'm using fullfile.
So here is the code I currently have, I do not know what the problem is.
auxiliaryData = input('Input Auxiliary Channel: ');
output = [Position,Hertz,Auxiliary_Channel_Power,Main_Channel_Power];
outputDir = '/home/mydirectory/locationIwantthefile';
dlmwrite(fullfile(outputDir,auxiliaryData),output, 'delimiter','\t','precision',10 );
The problem is "Could not open file"
The text files are an array of doubles.
So basically, how can I get my program to read in a read-only text file, output a text file in a specific location in my directory, and then have the name of the text file to be the name of the original text file which I inputted into my program?
0 个评论
回答(3 个)
Jan
2015-11-9
编辑:Jan
2015-11-9
This stores the typed value as a double:
auxiliaryData = input('Input Auxiliary Channel: ')
Perhaps you want:
auxiliaryData = input('Input Auxiliary Channel: ', 's')
to store the name as a string.
But this would be smarter:
outputDir = '/home/mydirectory/locationIwantthefile';
backDir = cd(outputDir);
[auxiliaryData, folder] = uiputfile('*.*', 'Input Auxiliary Channel: ');
if ~ischar(auxiliaryData)
error('User stopped file input.');
end
cd(backDir);
outFile = fullfile(folder, auxiliaryData);
Do you have write permissions in this folder? Is the file existing already and opened by another program or write protcected?
7 个评论
Walter Roberson
2015-11-11
When you use input() with the 's' option, do not use the '' around the string that you type in.
Jan
2015-11-11
编辑:Jan
2015-11-11
@Philip: input('...', 's') does work, if you use it correctly as specified. But it is a really bad idea to choose a file by this command. There are simply too many chances for typos and unexpected behavior, as you see in your own question. Using uigetfile or uiputfile would solve your problem. To me it seems, like you do not read our answers carefully. Your problem remains, that you do not specify an existing file or folder, and this is not surprising, when input() is used to define a file.
If you have tried the suggested uiputfile() method and state, that "it did not work", please post the corresponding code and explain, if an error occurres or the results differ from your expectations.
Walter Roberson
2015-11-9
You indicated in your other question that a typical response to the input() question would be
/home/pulsar/public_html/fscan/L1_DUAL_ARM/L1_DUAL_ARM_DCREADOUT_HANN/L1_DUAL_ARM_DCREADOUT_HANN/fscans_2015_10_01_06_00_02_CDT_Thu/L1_CAL-DELTAL_EXTERNAL_DQ/spec_0.00_100.00_L1_1127646019_1127732419.txt
However, if that were input then MATLAB would immediately complain about invalid syntax. Please remember that when you use input() without the 's' option then whatever string is entered is executed and the result of the execution is the value returned by input().
If you change to use the 's' option and the user is inputting the complete path like above, then you would just
dlmwrite(auxiliaryData, output, 'delimiter','\t','precision',10);
However I would in that case recommend you change the prompt, because to most of us asking for an input channel is asking for a channel number rather than a file name.
Image Analyst
2015-11-10
Philip, do not be cruel to your user and ask them to input a filename that way. Do it this way instead:
% Get the name of the file that the user wants to save.
startingFolder = '/home/mydirectory/locationIwantthefile';
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
dlmwrite(fullFileName, output, 'delimiter','\t','precision',10 );
(It's similar to what Jan said, except that I don't use cd().)
2 个评论
Jan
2015-11-11
@Philip: The error message means, that you type in a not existing file name. Teh double '//' looks e.g. suspicious. But because we cannot know the exact name of your file, we repeatedly suggest not to use the comand input to define the name of a file, because this is prone to errors. Better use uigetfile. This is the simple solution of your problem.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!