How to load a folder according to a vector into matlab?
Show older comments
Hello,
I have a matrix called perStimAtten (2x40):
perStimAtten =
-1 -1 -1 -1 -1 -2 -2 -2 -2 -2 -1 -2 -1 -1 -2 -2 -2 -1 -1 -2 -2 -1 -2 -1 -1 -1 -2 -1 -2 -2 -2 -1 -1 -1 -1 -2 -2 -2 -1 -2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
This matrix was build by matlab to create randomly sound stimulus, which stimulates neurons in mice. The pictures of the neurons captured by each stimulus refers to the array of the first row. I used this command:
[yy ii]=sortrows(perStimAtten');
to sort the rows into columns, and I got vector yy (40x2) which shows me that rows 1-20 of vector ii (40x1) were stimulated by stimulus number 2, and rows 21-40 were stimulated by stimulus number 1. Vector ii sorts the files (containing the pictures) in rows 1-40 so I can know which picture is connected to which stimulus.
Now I need to load to matlab the pictures taken when stimulus 1 was heard, and then when stimulus 2 was heard.
How do I draw the correct pictures according to vector ii?
Thank you! Sigal.
For convenience:
yy:
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-2 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
-1 0
ii:
6
7
8
9
10
12
15
16
17
20
21
23
27
29
30
31
36
37
38
40
1
2
3
4
5
11
13
14
18
19
22
24
25
26
28
32
33
34
35
39
5 Comments
Jan
on 23 Aug 2017
The readers cannot guess, how the corresponding picture can be found. You have provided a lot of details, which do not concern the problem in any way (sound stimulus, neurons, mice, sortrows, ...). But you do not share any detail about how the relation between the indices in ii allows to choose a picture.
Do you want to load all pictures or a specific one?
Sigal Cohen
on 23 Aug 2017
Jan
on 23 Aug 2017
@Sigal: No problem, you are welcome. It is usual that it is not clear to the asking person, what is exactly required to solve the problem: This is the nature of problems. :-)
Now please explain what is "picture 6"? Perhaps something like the file "picture0006.tiff" in the folder "D:\MeasurementData\2017_June_01\TestImages"?
You want to "load all pictures" - is loading the complete action you want to do? If so, why? Loading to what or where?
Does "when stimulus 1 was heard" mean that the corresponding yy(:,1) value is -2?
I still think, that the description of the actual problem can be expressed clearly in one sentence. Please try to find such a sentence, such that the answer allows you to solve the problem.
Sigal Cohen
on 27 Aug 2017
Jan
on 27 Aug 2017
@Sigal: Did you see my questions for clarifications? I would like to help you, but as long as I do not understand these points, I can't.
The error message you get is clear: You cannot use load to import a tif file. Use imread instead.
It is not meaningful to "load" all the data only. Explain, what should happen with the imported value. Then it will be much easier to help you to implement this.
Answers (1)
As per Jan's questions, it's really not clear what exactly you want to do. The following will load all the images in a cell array, as per (your badly named ii) order vector:
[stimulus, imageindices] = sortrows(perStimAtten');
imageroot = 'C:\Users\User\Desktop\Matlab\AnalysisScript\1';
images = cell(numel(imageindices), 1); %to store the images
for idx = 1:numel(imageindices)
imagename = sprintf('%d.tif', imageindices(idx));
images{idx} = imread(fullfile(imageroot, filename));
end
Do whatever you want with that cell array. Note that if all the images are the same size and with the same number of colour channels, then loading them into a 3D (greyscale images) or 4D matrix (RGB images) may be better.
Categories
Find more on Electrophysiology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!