How to load a folder according to a vector into matlab?

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

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?
I'm sorry I wasn't clear, my apologies.
The meaning of the indices in ii is: pictures 6, 7, 8, 9, 10, 12, 15... and so on until picture 40 (according to the list in ii) were taken while stimulus 2 was heard. pictures 1, 2, 3, 4, 5, 11, ...and so on until 39 were taken while stimulus 1 was heard.
I want to load all pictures that were taken when stimulus 1 was heard, and analyse them with a script I have. Then do the same with pictures that were taken when stimulus 1 was heard.
My pictures are tiff files.
If any further explanation is needed I'll be happy to provide.
Thank you,
Sigal.
@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.
Let's try this way:
This is what I have so far:
load('C:\Users\User\Desktop\sif files\Trial3\datafile6.mat');
[yy ii]=sortrows(perStimAtten');
for n=1:length(ii)
filename = sprintf('%d.tif', ii(n));
load(fullfile('C:\Users\User\Desktop\Matlab\AnalysisScript\1', filename));
end
But I get an error:
Error using load Unknown text on line number 1 of ASCII file C:\Users\User\Desktop\Matlab\AnalysisScript\1\6.tif "I*".
Error in AnalysisScript (line 14) load(fullfile('C:\Users\User\Desktop\Matlab\AnalysisScript\1', filename));
Which is odd because the file does exist on the path.
Maybe if I'll figure out what this error is I'll be able to proceed.
Thank you,
Sigal.
@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.

Sign in to comment.

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

Tags

Asked:

on 23 Aug 2017

Edited:

on 27 Aug 2017

Community Treasure Hunt

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

Start Hunting!