# How to print strings from a data text file that is in correlation to an indices text file?

2 views (last 30 days)
Akana Juliet on 15 Jun 2021
Edited: dpb on 16 Jun 2021
Hi everyone, I was hoping someone could help me with this tricky concept. I am trying to write a program and I don't really know how to execute it. You will see in the text files I've attached, I have one called index.txt and that has an array from 0 - 7, each index has 4 array elements (looks like this 0 1 6 2 3 1 3 2 4 0 2 1 6 4 7 ..etc). Then in the data.txt file I have another array 0 - 7 but each index includes a hexidecimal value. There are 3 categories, it should make sense when you view the text files. I'm looking for this to happen:
1.) User selects a category (Category 1, 2, and 3)
2.) User can either select index from 0-7 or an index at random.
3.) The program prints 4 strings(since the indices each contain 4 numbers) from the data.txt that is correlated to index file.
I was thinking to read line by line searching for "First Index, Second Index or Third Index to accomplish #1, and then maybe put the strings in cell arrays. This is honestly really complex for me, so any pointers on how I can get started on this would be greatly appreciated!

dpb on 15 Jun 2021
Edited: dpb on 16 Jun 2021
Put the indices into a 3D array and just index --
indices(:,:,1)=[1 6 2 3; 3 2 4 0; 1 6 4 7; 0 1 4 7; ...
0 5 6 1; 0 1 2 3; 0 5 6 2; 0 3 6 7];
indices(:,:,2)=[
0 6 2 3; 3 2 4 0; 1 6 4 7; 0 1 4 7; ...
0 5 6 1; 0 7 2 3; 0 5 6 2; 0 5 6 7]; % NB: presumed the last 4 0 5 6 7 ==>7 0 5 6 7 fixed typo
indices(:,:,3)=[
4 6 2 3; 3 2 4 0; 1 6 2 7; 0 1 4 7; ...
5 0 6 1; 0 1 2 3; 0 5 6 2; 0 5 6 0];
Do same thing with DATA array, then
icat=input('Enter category (1-3)'); % get the category
indx=input('Enter index (1-8)'); % and index (in MATLAB 1-based counting)
output=data(indices(indx,:,icat)); % retrieve data from icat plane, index row of lookup indices
dpb on 16 Jun 2021
Edited: dpb on 16 Jun 2021
Two reasons --
1. I added "1" to all the values so the indexing is one-based as are MATLAB arrays -- you can't change lower bounds in ML to be 0-based;
2. I removed the extra index variable that you had in the text file ahead of each 4-vector because it isn't needed when arranged by index by row and plane -- you just index into each plane with the row number 1 thru 8 for the wanted plane 1-3 and retrieve the four indices into the data array associated.
If you subtract "1" from the array, then you'll be able to see the comparison more easily because then you'll have the 0-7 indices that are in the text file--but, you have to add one to them to use as array indices, so may as well just convert on input "in anger".
The first hand-written looked like
indices(:,:,1)=[1 6 2 3; 3 2 4 0; 1 6 4 7; 0 1 4 7; ...
0 5 6 1; 0 1 2 3; 0 5 6 2; 0 3 6 7];
for the first plane -- you'll see [1 6 2 3] ==> [2 7 3 4] in the second and they're written out in 8x4 array display whereas I just edited in place keeping the two rows in writing them out explicitly before. They're the same array; if you display the above in command window w/o the trailing semicolon, it's an 8x4 array as well; the semicolons ensure that.
dpb on 16 Jun 2021
To show they're the same, I copied the code above for plane 1 to command line as I1 and
>> I1=[1 6 2 3; 3 2 4 0; 1 6 4 7; 0 1 4 7; ...
>> all(I1==indices(:,:,1),'all') % they aren't the same as is, agreed...
ans =
logical
0
>> all(I1==indices(:,:,1)-1,'all') % but they are the same when take off the "1"
ans =
logical
1
>>

### Categories

Find more on Logical in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!