Why am I unable to open file with dlmwrite?

12 views (last 30 days)
Real Name
Real Name on 8 Nov 2015
Edited: Jan on 11 Nov 2015
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?

Answers (3)

Jan on 9 Nov 2015
Edited: Jan on 9 Nov 2015
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.');
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?
Jan on 11 Nov 2015
@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.

Sign in to comment.

Walter Roberson
Walter Roberson on 9 Nov 2015
You indicated in your other question that a typical response to the input() question would be
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.
Real Name
Real Name on 10 Nov 2015
Your code won't work. It also doesn't solve my problem.

Sign in to comment.

Image Analyst
Image Analyst on 10 Nov 2015
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.
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().)
Jan on 11 Nov 2015
@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.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!