I am really a new person in MATLAB and I need use it to finish my homework.
First, I try to open a txt file to get data.
So, I do like this:
folder='C:\Users\yshi20\Desktop\COSC6335\proj_1';
file='transactionDB.txt';
myData=fullfile(folder,file);
[Datafile, message] = fopen('transactionDB.txt', 'r');
But the datafile value always show -1 which means it failed to open.
So, I use this to check why I cannot open it:
if Datafile < 0
disp(message);
c = [];
else
Data = fread(Datafile, 5, 'uint8=>char')'
end
But the result says: No such file or directory.
But I checked many times, and I am sure the file name is correct and the location folder is correct, so, how to solve the problem?
You're using the wrong variable. You need to use myData in fopen.
[Datafile, message] = fopen(myData, 'r');
myData stores the complete path to your file whereas in your original code, you're using relative referencing which means that it's going to look for the file in the current working directory. A -1 code means that it can't find the file... and rightfully so given your error. It can't find the file in the current working directory. As such, make sure you change your fopen statement so that the correct path to your file is specified.
Related
I keep running across this error in my code:
Path = 'C:\Users\18606\OneDrive\Documents\Spheroids For Brandon\Spheroids\1-8WT';
Content = dir(Path);
SubFold = Content([Content.isdir]); % Keep only the directories
MyData = [];
for k = 3:length(SubFold)
F = fullfile(Path, SubFold(k).name);
fileID = fopen(F);
MyData{end+1} = fopen(fileID,'%s\n');
fclose(fileID);
Resulting in the error:
Error using fopen
Invalid filename (line 8)
The code is trying to iterate over multiple images in multiple subfolders of the main. The goal is to process the images with an edge detection algorithm for each file, but that's besides the point. Why would the program give an invalid file name when the path, content and subfolders are all specified in the code? Do the variables mentioned have anything to do with the error? Finally, is there a better way to open and read the images iteratively?
Reading in files sequentially is indeed usually done through looping over a dir() call, i.e. your strategy is valid. What's going wrong here, is that Path is a path to a directory, not a file. SubFold then are only directories as they are found on the Path. fullfile(Path, SubFold(k).name) finally creates a path to a subdirectory of Path. A subdirectory is not a file, thus fopen will tell you that it's an incorrect filename.
You'll probably need another dir() call, e.g. dir(F) to get all files on the path specified by F.
My matlab function is in a folder that contains the main project and the other functions of the code. However, the data is stored in a folder withing the main one named "data" and inside the specific dataset that i want, for example "ded4" in this example. I can't figure out how to open the text file that I want without changing the file to the main folder. The code I have so far is:
function[Classify] = Classify(logDir)
%%%%logDir='ded014a04';
Directory = ['data/' logDir '/']
Filename = [logDir '-fixationsOffSet']
File_name = fullfile(Directory,Filename)
File = fopen(File_name,'r')
end
The code is in the 'dev' folder, I think my path is correct because when I do
open(File_name)
it opens.
Thanks for the help
If you want to open the file in the editor, use
open(File_name)
If you want to read data from the file, you can use
dlmread(File_name) % Read ASCII delimited file.
or
C = textscan(File,'FORMAT') % Read formatted data from text file or string.
or more low-level using fscanf, e.g., if the file contains three columns of integers you do the following: Read the values in column order, and transpose to match the appearance of the file: (from the help of fprintf)
fid = fopen('count.dat');
A = fscanf(fid,'%d',[3,inf])';
fclose(fid);
I'm using MATLAB R2017a and I would like to create a new binary file within the folder the script is running from.
I am running matlab as administrator since otherwise it has no permissions to create a file. The following returns a legal fileID:
fileID = fopen('mat.bin','w');
but the file is created in c:\windows\system32.
I then tried the following to create the file within the folder I have the script in:
filePath=fullfile(mfilename('fullpath'), 'mat.bin');
fileID = fopen(filePath,'w');
but I'm getting an invalid fileId (equals to -1).
the variable filePath is equal in runtime to
'D:\Dropbox\Studies\CurrentSemester\ImageProcessing\Matlab
Exercies\Chapter1\Ex4\mat.bin'
which seems valid to me.
I'd appreciate help figuring out what to do
The problem is that mfilename returns the path including the file name (without the extension). From the documentation,
p = mfilename('fullpath') returns the full path and name of the file in which the call occurs, not including the filename extension.
To keep the path to the folder only, use fileparts, whose first output is precisely that. So, in your code, you should use
filePath = fullfile(fileparts(mfilename('fullpath')), 'mat.bin');
This is probably a simple question, but I have some files and data. After printing each one of them to a '-dpng' file, I just want to view the plot and copy them elsewhere. However, opening the file, all I see is the "Import Wizard". I click "Finish" and nothing happens. This is my code:
files = dir('*.csv');
for file = files'
lab5 = csvread(file.name, 9)
lab5(:,1) = log10(lab5(:,1))
plot(lab5(:,1),lab5(:,2))
print(strcat(file.name,'plot'), '-dpng')
end
I tried to avoid print() by using savefig, but for some reason savefig was giving me a vague error. Only print works, but I'm not sure how to view the output.
You are saving your image as filename.csvplot, which Preview does not accept as a valid image file.
For example:
% Generate dummy file
fID = fopen('blah.csv', 'w');
fclose(fID);
% Recreate print job
files = dir('*.csv');
plot(1:10)
fname = strcat(files(1).name, 'plot');
print(fname, '-dpng');
Which gives us:
fname =
blah.csvplot
Why isn't .png appended to the filename? Per the documentation for print:
If the file name does not include an extension, then print appends the appropriate one.
Filename inputs are parsed for an extension (e.g. things prepended with .), and does not append an extension if one is found. In this case, the filename passed to print has the .csvplot extension. This might be unexpected but it does make sense, file extensions don't actually control anything about the file itself; you could save your file as image.finderpleaseopen and have it still be a valid PNG file. Finder is just too stubborn to open it without being forced because it's not a known, supported file extension.
To fix this, you should save your file with the correct file extension. There are two ways to do this, append the correct extension or remove the undesired extension with something like fileparts or regexprep and let print handle it for you.
For example:
% blah.csvplot.png
fname = strcat(files(1).name, 'plot', '.png');
print(fname, '-dpng');
% blahplot.png
[~, filename] = fileparts(files(1).name);
fname = strcat(filename, 'plot');
print(fname, '-dpng');
savefig does not produce a valid output for Finder because it does not produce any output without a .fig extension:
If the specified file name does not include a .fig file extension, then MATLAB appends the extension. savefig does not accept other file extensions.
*.fig files are not image files and cannot be opened natively by finder.
I've been trying to write image files to the specified folder, but I always receive this error:
Unable to open file
"C:\Users\Dani\Desktop\code_version_1.0\myImages"
for writing. You may not have
write permission.
Is there a way to fix this? Thanks.
for i=1:numberOfFiles
filename=fileList{i};
img=imread(filename,'jpg');
image = imresize(img, [150,150]);
folder='C:\Users\Dani\Desktop\code_version_1.0\myImages';
if ~exist(folder,'dir')
mkdir(folder);
end
imwrite(image,folder,'jpg');
end
Your call to imwrite has an invalid second parameter. You gave it a folder when it is asking for a file path.
Here's a possible work-around:
outfile = fullfile(folder, 'output.jpg');
imwrite(image, outfile, 'jpg');
How about using the imwrite like this :
imwrite(image,'C:\Users\Dani\Desktop\code_version_1.0\myImages\image.jpg');
you can after append things as you like. check this link