I have a file a.txt:
03,17.406199
05,14.580129
07,13.904058
11,14.685388
15,14.062603
20,14.364573
25,18.035175
30,21.681789
50,22.662820
The number of rows in the file are not known. I want to read the file and store
3
5
7
11
15
20
30
50
in one array and the float values in another.
How do I read in a file when the length of data is not known?
If the number of entries is the same in every row, and if all the entries are numeric, then
you can simply do
a = load('a.txt');
a will be a matrix with two columns.
Read line-by-line until you hit the EOF marker.
Certain functions (like TEXTSCAN) will continue recycling the format string until the end of the file is reached. Other functions (like FSCANF) can take Inf as a size option, indicating that it should continue reading until the end of the file. If you are reading data line-by-line in a loop, you can use the FEOF function to test if the end of the file has been reached.
Since your elements are separated by commas, take a look at csvread. This should read the entire file into a single matrix, which you can then split into the two vectors you want.
Disclaimer: not tested!
fileContents = csvread('a.txt');
integerColumn = fileContents(:, 1);
doubleColumn = fileContents(:, 2);
Related
I need to read binary which contains 3d dots coordinates but first there are 5 lines which are written to file in normal mode, so first I need to skip that part and read the dots. I tried the fread but failed. What am i doing wrong
VERSION 1
DOTS x y z
DOTCOUNT 10
DATA binary
33ËB3³ÊB33ÊBfæÊBffÊBfæÉBš™ÊBšÊBš™ÉBÍLÊBÍÌÉBÍLÉB
You are actually looking for the data after the first five '\n' characters.
You can read the first 256 bytes of the file, and look for the new lines. if they are there, you start reading your binary data, right after the fifth occurrence. if you did not find five '\n' character, continue reading the next chunk of 256 bytes, and look for the remaining number of '\n' characters. The binary data shall be read after the first five lines were consumed.
That's all
I am having trouble trying to get the information from my .txt file to MATLAB into an array so I can process the data. Here is my current code
fileID = fopen(filename,'r');%assume filename is a generic name for my file
sizeData = [256 inf];
formatSpec = '%f';
OriginalData = fscanf(fileID,formatSpec,sizaData); %OriginalData is the array
My .txt file looks something like this...
The size of the information is 256 x 256 array. I can't quite figure out why my OriginalData array is showing nothing. Any help will be greatly appreciated. Thank you.
You have a bunch of text first followed by the numeric data. Reading this data as is won't work because your format specifiers are expecting floating point numbers at the very beginning of your text file, yet you have a bunch of text that isn't floating point data instead.
Skip the text first then read in the data after the text. Try doing:
OriginalData = dlmread(filename, ' ', 31, 0);
dlmread reads numeric text data from a file and imports it into MATLAB. The first parameter is the file you want to read, the second is the delimiter that separates numbers, which is the space for your case, then we want to skip over 31 rows, and have a no column offset - denoted by the third and fourth parameters respectively.
Say I'm calling a program:
$ ./dataset < filename
where filename is any file with x amount of line pairs where the first line contains a string and second line contains 10 numbers separated by spaces. The last line ends with "END"
How can I then start putting the first lines of pairs (string) into:
char *experiments[20] // max of 20 pairs
and the second lines of the pairs (numbers) into:
int data[10][20] // max of 20, 10 integers each
Any guidance? I don't even understand how I'm supposed to scan the file into my arrays.
Update:
So say this is my file:
Test One
0 1 2 3 4 5 6 7 8 9
END
Then redirecting this file would mean if I want to put the first line into my *experiments, that I would need to scan it as such?
scanf("%s", *experiments[0]);
Doing so gives me an error: Segmentation fault (core dumped)
What is incorrect about this?
Say my file is simply numbers, for ex:
0 1 2 3 4 5 6 7 8 9
Then,
scanf("%d", data[0][0]); works, and will hold value of '1'. Is there an easier way to do this for the whole line of data? i.e. data[0-9][0].
find the pseudo-code, code explains how to read the input
int main()
{
char str[100]; // make sure that this size is enough to hold the single line
int no_line=1;
while(gets(str) != NULL && strcmp(str,"END"))
{
if(no_line % 2 == 0)
{
/*read integer values from the string "str" using sscanf, sscanf can be called in a loop with %d untill it fails */
}
else
{
/*strore string in your variable "experiments" , before copying allocate a memory for the each entry */
}
no_line++;
}
}
The redirected file is associated with the FILE * stdin. It's already opened for you...
otherwise, you can treat it the same as any other text file, and/or use the functions that are dedicated to standard input - with the only exception that you cannot seek in the file and not retrieve the size of the input.
For the data sizes you're talking about, by far the easiest thing to do is just slurp all of the content into a buffer and work on that: you don't have to be super-stingy, just make sure that you don't overrun.
If you want to be super-stingy with memory, preallocate a 4kB buffer with malloc(), progressively read() into it from stdin, and realloc() another 4kB every time the input exceeds what you've already read. If you don't care so much about being stingy with memory (e.g. on a modern machine with gigabytes of memory), just malloc() something much bigger than the expected input (e.g. a megabyte) and bug out if the input is more than that: this is far simpler to implement but less general/elegant.
You then have all of the input in a buffer and you can do what you like with it, which depends too strongly on the format of the input for me to say how you should approach that part.
I am a newbie to programming in postscript directly.
I am reading from a file and I am using the read command to parse symbols.
Most of the symbols I am checking for are 2 characters in length and one is 3 characters in length. I would change the one that is 3 characters in length into two characters, but that won't help for the following reasons:
The symbols are a standard so I can't say.. well, thanks for making
just one of them 3 characters unlike all the rest!
The symbol that has 3 characters has the first 2 characters the same as another symbol.
I need to be able to "peek" at the next character in the file if the first two symbols match and not modify the the read order if the character peeked at doesn't match what I want to make the 3 character symbol.
Example:
File text as a string: "AB3;ABB4;"
In this case I will read A, then B and because the two characters together are "AB", I need to see if reading the next character produces a B.. if not, I don't want to have modified my reading order so I can proceed in the code normally to extracting the value. If it does, I now have my 3 character symbol and can proceed to extract the value normally.
Thank you.
Postscript doesn't have an unget function, so you can't push a byte back onto the stream to read it again later. But, depending upon OS-support, Level 2 Postscript lets you reposition a file. So you could implement your peek function like this:
% file peek int true
% false
/peek {
dup fileposition exch dup % pos file file
read { %true: pos file int
3 1 roll exch setfileposition true
}{ %false: pos file
pop pop false
} ifelse
} def
I have these a file in a c program which consist of a string and 4 doubles and 2 integer in one line and there is a total of 28 lines, i want to read this file and load the data into an array. can someone help me solve this.
Split up your problem into sub-tasks:
Open the file using fopen
Allocate a buffer (array) to store the doubles
Create a loop while there is more to read
read in a single double into the array
Go to the next iteration