I'm trying to save all the files in a directory as an array of strings, like this:
files = {'hello.gdf'; 'hello2.gdf'...; ... 'etc.gdf'}
Since I have many directories, I want to do this automatically. This is my code:
gdffiles = dir(fullfile('D:', 'subject', '01', '*.gdf'))
for i=1:size(gdffiles)
files(i) = gdffiles(i).name;
end
I want to assign to files the name of the gdf files found, but I get this message:
??? Subscripted assignment dimension mismatch.
Error in ==> getFiles at 3
files(i) = gdffiles(i).name;
What am I doing wrong? Thanks!
The reason for error:
You try to assign files in the i-th place a string (char array) gdffiles(i).name. However, you are using an array-element assignment (round parenthesis ()). Therefore, you get an error: You can only assign a single char using files(i).
Possible solutions:
You should assign to files using curly braces - since files is a cell array:
files{i} = gdffiles(i).name;
You can achieve the same result without the loop by:
files = { gdffiles(:).name };
Check this solution
path = fullfile('D:', 'subject', '01', '*.gdf');
files = dir(path);
files = struct2cell(files);
files = files( 1, 1:end );
Have you tried this :
ListOfAllFiles = ls('*.gif')
Hope it helps
Related
Using JEXL, I am trying to initialize array and than adding elements into it, however below code gives me 'unsolvable property '0' error.
var abc=[];
abc[0]=5;
1) How can I initialize empty array and keep adding values in it?
2) Can I use it like List, where I do not need to specify size at the time of initialization ?
in JEXL syntax you can initialize objects with new function.
Other option is to add to context arraylist:
This is a working example with jexl2:
JexlEngine jexl = new JexlEngine();
String jexlExp = "var abc=new(\"java.util.ArrayList\", 1);abc[0]=5";
Expression e = jexl.createExpression( jexlExp );
List<Integer> abc = new ArrayList<>(1);
JexlContext jc = new MapContext();
//jc.set("abc", abc ); second option to add arraylist to context
Object o = e.evaluate(jc);
In JEXL, the syntax [] creates a Java array, not a List. As an array, it has a fixed size, so you cannot add values to it. However, JEXL 3.2 has a new syntax for creating an ArrayList literal. Basically, you add ... as the final element.
So in JEXL 3.2, your example could be written as:
var abc=[...];
abc.add(5);
See the JEXL literal syntax reference for more information.
I face this issue and can't seem to find a fix except with Scipy or Numpy, both of which I don't wanna use in this case.
From a .csv file, I want to extract the values of the first column :
enter image description here
Which I manage to do with the following code :
mat_data = open('file.csv')
data_reader = csv.reader(mat_data)
list_data = list(data_reader)
value1=float(list_data[1][0])
value2=float(list_data[2][0])
value3=float(list_data[3][0])
I'd now like to create a loop that could be used and create value"i" no matter how many lines long my .csv is.
Any idea?
This did the trick for me !
mat_data = open('file.csv')
data_reader = csv.reader(mat_data)
list_data = list(data_reader)
i=0
value=dict()
for i in range(1,len(list_data)):
value[i]=list_data[i][0]
print value[i]
maybe I'm doing it the wrong way, but I want to combine 2 arrays of different length in one object, so I use this code:
function MyArrayOfFilesAndFolders(){
var folders = [my array of folders] // 60 items
var files = [my array of files] // 220 items
var res = {
folders: folders,
files: files
}
return res
}
The resulting object has 60 items for folders, but only 100 items for files (despite the fact that the original array "files" contains 220 items).
There's something wrong in this method or is a GAS bug?
I'm missing something else?
Thanks for any help
Like Jonathon says, the debugger has a limit on the length of the arrays and in some cases it truncates long arrays to 100 items.
To test the real length of an array, it's better to use Logger.log("number of items: " + array.length).
Not sure how you want it to display the values... Does this help?:
function MyArrayOfFilesAndFolders(){
var folders = ["I", "Am", "Robot"];
var files = ["Yes","In","Deed"]
var res = folders+files;
Logger.log(res)
}
If it doesn't can you be more specific?
Cheers!
I have a variable that is a csv list and i am trying to convert it into an array using array_map but for some reason it keeps giving me the following error:
Warning: array_map(): Argument #2 should be an array in
$list1 = 1,7,15,16,18,18;
$shortArray = array_map('str_getcsv', $list1);
$var_dump($shortArray);
Does anyone know how to get this to work or is there another way to convert a csv list into an array?
I am using php 5.5.0.
$list1 = array(1,7,15,16,18,18);
$shortArray = array_map('str_getcsv', $list1);
var_dump($shortArray);
$list1 = "1,7,15,16,18,18"; // or the line from your CVS file
$shortArray = explode(",", $list1);
var_dump($shortArray);
My code has 2 parts. First part is an automatic file opening programmed like this :
fichierref = 'H:\MATLAB\Archive_08112012';
files = dir(fullfile(fichierref, '*.txt'));
numberOfFiles = numel(files);
delimiterIn = ' ';
headerlinesIn = 11;
for d = 1:numberOfFiles
filenames(d) = cellstr(files(d).name);
end
for i=1:numberOfFiles
data = importdata(fullfile(fichierref,filenames{i}),delimiterIn,headerlinesIn);
end
Later on, I want the user to select his files for analysis. There's a problem with this though. I typed the lines as follow :
reference = warndlg('Choose the files from which you want to know the magnetic field');
uiwait(reference);
filenames = uigetfile('./*.txt','MultiSelect', 'on');
numberOfFiles = numel(filenames);
delimiterIn = ' ';
headerlinesIn = 11;
It's giving me the following error, after I press OK on the prompt:
Cell contents reference from a non-cell array object.
Error in FreqVSChampB_no_spec (line 149)
data=importdata(filenames{1},delimiterIn,headerlinesIn);
I didn't get the chance to select any text document. Anyone has an idea why it's doing that?
uigetfile is a bit of an annoying when used with `MultiSelect': when you select multiple files the output is returned as a cell array (of strings). However, when only one file is selected the output is of type string (not a cell array with a single cell, as one would have expected).
So, in order to fix this:
filenames = uigetfile('./*.txt','MultiSelect', 'on');
if ~iscell(filenames) && ischar( a )
filenames = {filenames}; % force it to be a cell array of strings
end
% continue your code here treating filenames as cell array of strings.
EDIT:
As pointed out by #Sam one MUST verify that the user did not press 'cancel' on the UI (by checking that filenames is a string).