MATLAB Answers

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

1 view (last 30 days)
Akana Juliet
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!

Accepted Answer

dpb
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
  6 Comments
dpb
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
>>

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by