File format of 2d array octave - arrays

I have the following 2d array:
1 2 3
4 5 6
7 8 9
stored in a text file in format: [1 2 3; 4 5 6; 7 8 9;]. However when I try to load this file and save to a variable using:
a = load('data.txt'), it gives me following error:
error: load: unable to determine file format of 'data.txt'
Any suggestion on this would be nice. Thanks.

load only handles ASCII data if it's in the format shown in the first part of your post.
data.txt
1 2 3
4 5 6
7 8 9
And read it using:
data = load('data.txt', '-ascii');
If your data is stored as a formatted string rather than the ASCII matrix shown above, you'll have to read the file in a string and then use str2num to convert it to a 2D array.
fid = fopen('data.txt', 'r');
data = str2num(fread(fid, '*char').');
fclose(fid);
In the future, I would recommend storing matrices as ASCII as shown in the top part of the post

Related

How to process array data in chunks where last chunk may be a partial size

I have array of integer and I am trying to send this array as a sub block from esp32 to another one.
According this code I get on output like this:
output:
1 2 3 4 5
6 7 8 9 10
11 12 0 0 0
the expected output:
1 2 3 4 5
6 7 8 9 10
11 12
How can I update on esp_now_send to get like the expected output? how can I deal with the last sub block if it is less than 5 numbers?
The code needs to send up to only the available data. To do that the general approach would be to send full sub-blocks until the last sub-block which may be a partial one. That can be determined by simple maths logic to work out how much the current iteration should send based on how much data is left.
The code changes would be:
Change siz to be the real number of entries in the array: siz = sizeof(data)/sizeof(data[0]).
Change rang in the function call to `(ind + rang <= size ? rang : size - ind)``. That is, the size passed to the function call depends on how much data is left.

reading a 3 dimensional array from txt file

I am having a problem storing a 3 dimensional array into a proper format.
The text file reads as follows:
4 3 5 4 3 2
3 5 4 3 2 3
1 0 4 3 2 4
2 3
1 5
1 6
3 5
the integers are standard integers and need to be arranged as they appear. So the upper part of the txt file will be stored into a 3x3 matrix with each cell containing the two integer individual values. the bottom values should be stored in a 1x4 matrix with each cell containing the 2 individual integer values.

Get values of columns of a file with tabs and newlines separating them read via bash script to an array

Hello I have been trying to get the numbers in the columns of a file for two days by reading a file via a bash script. Here is the file sample.txt
1 1 1 1 1
9 3 4 5 5
6 7 8 9 7
3 6 8 9 1
3 4 2 1 4
6 4 4 7 7
By column I mean i.e the first column is
1
9
6
3
3
6
I need to have the column elements each be in a given array col1 or col2 etc so that I can manipulate the values further.
Here's what I have done so far using while loop I have read the contents of the file assigning them each line to an array.
If I set IFS=$'\n'
while read -a line
do
IFS=$'\n'
#I can get the whole column 1 with this
echo ${line[0]}
#for column 2 I can get it by this an the others too
echo ${line[1]}
done < sample.txt
Now that may seem good as i thought but since I want to calculate averages of the columns putting in another loop like a for loop becomes impossible since ${line[0]} has all the elements in column 1 but they are all as a single string (i have tried to observe) that cannot be acted upon.
What would be the best way to get those elements be members of a given array and then compute the averages on them. help appreciated .
In bash I'd write
declare -A cols
n=0
while read -ra fields; do
for ((i=0; i<${#fields[#]}; i++)); do
cols[$i,$n]=${fields[i]}
((n[i]++))
done
done < sample.txt
read -a reads the fields of the line into the named array.
I'm using cols as an associative array to fake a multi-dimensional array. That's way easier to deal with than using a dynamic variable name:
eval "column${i}[$n]=\${fields[$i]}"

Read Text File of integers into One 2D matrix in Matlab

I Matlab I want to read a File of the following format :
1 23 6 547 8 9 .....
56 56 85 2 7 9 ....
I want to read this file and store it in a 2D array that has the same shape as the file. Reading in Matlab seems complicated as it has conversions ratios! and other stuff
Any Help
Maybe you are looking for the function importdata
importdata('test.txt')
ans =
1 2
3 4
You can also use load (assuming a text file named 'a'):
load a

What format does matlab need for n-dimensional data input?

I have a 4-dimensional dictionary I made with a Python script for a data mining project I'm working on, and I want to read the data into Matlab to do some statistical tests on the data.
To read a 2-dimensional matrix is trivial. I figured that since my first dimension is only 4-deep, I could just write each slice of it out to a separate file (4 files total) with each file having many 2-dimensional slices, looking something like this:
2 3 6
4 5 8
6 7 3
1 4 3
6 6 7
8 9 0
This however does not work, and matlab reads it as a single continuous 6 x 3 matrix. I even took a look a dlmread but could not figure out how to get it do what I wanted. How do I format this so I can put 3 (or preferably more) dimensions in a single file?
A simple solution is to create a file with two lines only: the first line contains the target array size, the second line contains all your data. Then, all you need to do is reshape the data.
Say your file is
3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
You do the following to read the array into the variable data
fid = fopen('myFile'); %# open the file (don't forget the extension)
arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers
data = str2num(fgetl(fid)); %# read the second line
data = reshape(data,arraySize); %# reshape the data
fclose(fid); %# close the file
Have a look at data to see how Matlab orders elements in multidimensional arrays.
Matlab stores data column wise. So from your example (assuming its a 3x2x3 matrix), matlab will store it as first, second and third column from the first "slice", followed by the first, second third columns from the second slice and so on like this
2
4
3
5
6
8
6
1
7
4
3
3
6
8
6
9
7
0
So you can write the data out like this from python (I don't know how) and then read it into matlab. Then you can reshape it back into a 3x2x3 matrix and you'll retain your correct ordering.

Resources