MATLAB dir command not working properly - matlab

I have a folder containing 9 .htk files. I need to use "dir", and then "readhtk" in a loop to import them to MATLAB, but DIR appears to give 10 files instead of 9! here is my code:
htkfiles = dir('/Users/Desktop/Acsegment/mfcdir/*.htk');
nhtkfiles = length(htkfiles); % 10!!! It should be 9 tough!
data = cell(nhtkfiles,2);
for k = 1:nhtkfiles
b(k,1) = strcat({'/Users/Desktop/Acsegment/mfcdir/'},{htkfiles(k,1).name});
eval(['data{k,1} = readhtk(b{k,1});']);
end
When looking at the filenames in htkfiles, I have them like this:
htkfiles(1,1).name = '.htk'
htkfiles(2,1).name = 'fadg0_si1279.htk'
htkfiles(3,1).name = 'fadg0_si1909.htk'
htkfiles(4,1).name = 'fadg0_si649.htk'
htkfiles(5,1).name = 'fadg0_sx109.htk'
htkfiles(6,1).name = 'fadg0_sx19.htk'
htkfiles(7,1).name = 'fadg0_sx199.htk'
htkfiles(8,1).name = 'fadg0_sx289.htk'
htkfiles(9,1).name = 'fadg0_sx379.htk'
htkfiles(10,1).name = 'faks0_si943.htk'
Comparing to what I see in that folder, the first file is not supposed to be there! Anyone got any ideas why Im getting one extra file?

As mentioned in the comments: the dir command actually works properly, there just happens to be a hidden file.
These files starting with a dot could be removed from your list like so:
d=dir;
d(strncmp({d.name},'.',1))=[];

Related

loop through non-hidden files matlab

I'm trying to loop through non-hidden files in a dir and add them to a cell but for some reason it still preserves the indexing of the hidden files.
Number of non-hidden files in png_dir_path = 25
listing = dir(png_dirpath);
ecell = {};
for i=1:length(listing)
name = listing(i).name
if ~strncmp(name, '.', 1) % No files starting with '.'
disp(listing(i).name)
ecell{i} = fullfile(png_dirpath,listing(i).name);
end
end
When I run the above, I get an ecell of size 1x28 but the disp() returns only the hidden filenames. Why is it that when adding the filenames to the cell, it also adds the '.', '..' and '.DS_Store' ?
Thank you!!
Contents of png_dirpath
MBP:cartoon_png$ ls
car1.png car12.png car15.png car18.png car20.png car23.png car3.png car6.png car9.png
car10.png car13.png car16.png car19.png car21.png car24.png car4.png car7.png
car11.png car14.png car17.png car2.png car22.png car25.png car5.png car8.png
Output of script:
>> load_cartoon
name =
.
name =
..
name =
.DS_Store
name =
car1.png
car1.png
name =
car10.png
car10.png
name =
car11.png
car11.png
name =
car12.png
car12.png
name =
car13.png
car13.png
name =
car14.png
car14.png
name =
car15.png
car15.png
name =
car16.png
car16.png
name =
car17.png
car17.png
name =
car18.png
car18.png
name =
car19.png
car19.png
name =
car2.png
car2.png
name =
car20.png
car20.png
name =
car21.png
car21.png
name =
car22.png
car22.png
name =
car23.png
car23.png
name =
car24.png
car24.png
name =
car25.png
car25.png
name =
car3.png
car3.png
name =
car4.png
car4.png
name =
car5.png
car5.png
name =
car6.png
car6.png
name =
car7.png
car7.png
name =
car8.png
car8.png
name =
car9.png
car9.png
As you can see in the above output of the script, the first row in name is with hidden files and the 2nd row is without hidden files.
Mad Physicist's answer is correct as to why you are having that issue.
However, it looks like there is a much simpler way to just get a listing of PNGs in a directory if that is your end goal.
listing = dir(fullfile(png_dirpath,'*.png'));
ecell = {listing.name};
No for loop required or directory checking.
The problem is most likely that you are moving the index into ecell in lockstep with your index in ls. Unless all your hidden files are at the end of the listing, ecell will be the same length as ls. You can use #LuisMendo's suggestion to remove the lockstep:
listing = dir(png_dirpath);
ecell = {};
for item = listing'
if item.name(1) == '.'
ecell{end + 1} = fullfile(png_dirpath, item.name);
end
end
Part of the problem is that you are basing your conclusion that the hidden files are still present on the fact that ecell is 28 elements long. If you had looked at the contents, you would have realized that your algorithm is almost correct. Debugging is a learned skill. It is very easy to learn how to look (you had the right idea with the printouts), but it is very difficult to learn where to look.

Movefile when string is a variable

This is within a forloop aimed at moving multiple files within multiple folders into the 'RawData' directory. I am having issues with using movefile. I would like the file being moved to be a varaible and not a string typed into the command.
Here is a portion of the folder list if needed to better understand:
'Data-20141003T091843-1-Eyes Open on Flat Surface-Force.csv'
'Data-20141003T091843-1-Eyes Open on Flat Surface-Results.csv'
'Data-20141003T091923-2-Eyes Closed on Flat Surface-Force.csv'
'Data-20141003T091923-2-Eyes Closed on Flat Surface-Info.csv'
'Data-20141003T091923-2-Eyes Closed on Flat Surface-Results.csv'
'Data-20141003T092208-3-Limits of Stability-Force.csv'
'Data-20141003T092208-3-Limits of Stability-Info.csv'
>>foldername = foldername.name;
directoryname = 'C:\Users\murphy\Documents\MATLAB\RawData\';
folderdirectory = strcat(directoryname,foldername);
cd(folderdirectory);
folderdir = dir('*.csv');
folderList = {folderdir.name}'; %List of Files in folder
for f = 1:length(folderList) **movefile(folderList(f),'C:\Users\murphy\Documents\MATLAB\RawData')**
end
folderList is a cellstr.
In your for loop you need to pass folderList{f} instead of folderList(f).
for f = 1:length(folderList)
movefile(folderList{f},'C:\Users\murphy\Documents\MATLAB\RawData')
end

Matlab - open netcdf files from only specific subfolders

I have several subfolders in my parentFolder but I only want to read the .nc files from a few specific subfolders. I thought I had it figured out with this:
parentFolder = '/Users/AM/workspace/';
cd(parentFolder)
s1 = 'daytime/instant';
s2 = 'daytime/monthly';
s3 = 'nighttime/instant';
s4 = 'nightttime/monthly';
sub_f = {s1,s2,s3,s4};
s = strcat(parentFolder, sub_f);
% 1x4 cell with pathnames to files
filePattern = fullfile(s, '*.nc');
ncFiles1 = cell(length(filePattern),1);
for k = 1:4
ncFiles1{k} = dir(filePattern{k});
end
ncFiles = [ncFiles1{1}; ncFiles1{2}; ncFiles1{3}; ncFiles1{4}];
filename = cell(length(ncFiles),1);
for k = 1:length(ncFiles)
filename{k} = ncFiles(k).name;
end
I ended up with a 131x1 cell array of all the filenames I wanted, but when I tried to read the data I got this error message:
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open instant_SR_CR_DR_2008-07-01T00-15-56ZD_day_CFMIP2_2.90.nc for reading.
I know the files are okay because I can read them in by going to each subfolder individually. Is there a better way to read files out of specific subfolders? Any reason I'm getting that error when I try to open the files?
Thanks in advance!

Octave: Problems with load

I'm currently doing a program in Octave where I want the user to be able to insert the file that he wants to load. The files in question are .mat files and are loaded with
load ("filename.mat")
I was thinking about doing something like this:
file=input("Whats the file name: ")
load ("file")
But that didn't work...
Anyone got any tips?
That's likely because you need to input the file name enclosed in single quotation marks : 'filename'. (Note: I use MATLAB but that should work just the same in Octave).
As an alternative you can use inputdlg to request user input. It gives you much flexibility as you can add fields to the prompt such as the file extension or else.
Here is a simple example:
clear
clc
prompt = {'Enter file name'};
dlg_title = 'Input';
num_lines = 1;
def = {'Dummy file'};
answer = inputdlg(prompt,dlg_title,num_lines,def)
The prompt looks like this:
You can fetch the asnwer like so:
name = answer{1};
And finally add the extension to load the .mat file:
filename = strcat(name,'.mat')
S = load(filename)
To do it in one go with the file extension:
prompt = {'Enter file name'; 'Enter file extension'};
dlg_title = 'Input';
num_lines = 1;
def = {'Dummy file'; '.mat'};
answer = inputdlg(prompt,dlg_title,num_lines,def)
name = answer{1};
extension = answer{2};
filename = strcat(name,extension)
S = load(filename)
Hope that helps!
I used Benoit_11's method but changed it to input instead since inputdlg doesn't seem to work in Octave.
clear
clc
name=input('Enter the file name, without the file extension: ','s')
filename = strcat(name,'.mat')
S = load(filename)

Writing image files to folder in matlab

Below is some code which is showing error
imgIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
rgbBlock=ca{r,c};
imagename=strcat(int2str(imgIndex), '.jpg');
name=strcat((int2str(our_images)),'\',imagename);
imwrite(rgbBlock,'name');
I am trying to write some image file to folder using imwrite. But the last 3 lines is showing error. I need to save all the images which I have created.
I imagine that our_images is the name of the folder when you want your images, so instead of this:
name=strcat((int2str(our_images)),'\',imagename);
you need this:
name=strcat(our_images,'/',imagename);
Also look out for the backslash in there.
Also your name variable should not be quoted here:
imwrite(rgbBlock,'name');
So instead it should be:
imwrite(rgbBlock, name);