Why am I unable to open file with dlmwrite?

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
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.');
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 Comments

No, I want it to be stored as a double, because like I said, the input is a text file of an array of doubles.
The file is write protected. I'm not sure why I'd need an if statement. My code was working before without fullfile. However, I added it there because I want the name of the output to be the name of my input.
I cannot follow you. The 's' flag in the input command concerns only the name of the file, not the file's contents.
Oh sorry, I thought it was used for taking an input of strings. However, I tried it and it won't work. This is my error when I use the code that I know does work, but replacing input('...','s')
Input Main Channel: '/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'
Input Auxiliary Channel: '/home/pulsar/public_html/fscan/L1_DUAL_ARM/L1_PEM/L1_PEM/fscans_2015_10_01_06_00_03_CDT_Thu/L1_PEM-CS_MIC_LVEA_INPUTOPTICS_DQ/spec_0.00_100.00_L1_1127646019_1127732420.txt'
Error using load
Unable to read file ''/home/pulsar/public_html/fscan/L1_DUAL_ARM/L1_PEM/L1_PEM/fscans_2015_10_01_06_00_03_CDT_Thu/L1_PEM-CS_MIC_LVEA_INPUTOPTICS_DQ/spec_0.00_100.00_L1_1127646019_1127732420.txt'': no such file
or directory.
Error in powerFinder (line 4)
data1 = load(auxiliaryData);
Jan and I both told you not to use input() at all and to use uiputfile() like we told you (this is no coincidence), but you're not taking our advice. Good luck though with your methods.
Like I already said, I tried it and it didn't work. You don't seem to be reading my comments. I specifically said:
  1. Input('...','s'); does not work and I gave you the output
  2. Your method does not work.
When you use input() with the 's' option, do not use the '' around the string that you type in.
Jan
Jan on 11 Nov 2015
Edited: 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.

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.

4 Comments

Real Name
Real Name on 9 Nov 2015
Edited: Real Name on 9 Nov 2015
Why would it complain about invalid syntax? This worked before, but I did not have fullfile.
I'm not entering a string, I'm entering a textfile that has an array of doubles. So why would I put the s option?
Your code won't work because it'll output the data to auxiliaryData which is write protected.
>> auxiliaryData = input('Input Auxiliary Channel: ')
Input Auxiliary Channel: /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
/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
|
Error: Unexpected MATLAB operator.
I'm not sure what you mean by that, my code works without the fullfile part of the code.
You need to put the input in ' single quotes.
Your code won't work. It also doesn't solve my problem.

Sign in to comment.

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 Comments

The files are scattered all over the place. Could you help me explain what your code you've shown is doing? I'm not sure why you'd have an if statement there. I'd think the solution would be pretty simple.
It's giving me an error when I use your code:
Input Main Channel: '/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'
Input Auxiliary Channel: '/home/pulsar//fscan/L1_DUAL_ARM/L1_PEM/L1_PEM/fscans_2015_10_01_06_00_03_CDT_Thu/L1_PEM-CS_MIC_LVEA_INPUTOPTICS_DQ/spec_0.00_100.00_L1_1127646019_1127732420.txt'
Error using load
Unable to read file '/home/pulsar//fscan/L1_DUAL_ARM/L1_PEM/L1_PEM/fscans_2015_10_01_06_00_03_CDT_Thu/L1_PEM-CS_MIC_LVEA_INPUTOPTICS_DQ/spec_0.00_100.00_L1_1127646019_1127732420.txt': no such file or directory.
@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.

Categories

Tags

Asked:

on 8 Nov 2015

Edited:

Jan
on 11 Nov 2015

Community Treasure Hunt

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

Start Hunting!