Error encoding with Huffman in Matlab - matlab

I have a file where i can read the next example of characters:
HP†d€H EPŠ€E iEPƒ1€E OGP†d€G HP†d€H JP†d€J LP†d€L.......
And when I try to encode the samples of the file using the function of Matlab "huffmanenco" Matlab says me this:
Error using huffmanenco (line 86)
The Huffman dictionary provided does not have the codes for all the input
signals.
Error in RETO2 (line 35)
enco=huffmanenco(double(x),dict);
The next code is the one I'm using:
fileID = fopen('reto2014.mid');
[x,cont]=fscanf(fileID,'%c');
fclose(fileID);
y=[1:256];
va = hist(double(x),y);
prob= va./cont;
bar(y,prob)
[dict,avglen]=huffmandict(y,prob);
enco=huffmanenco(double(x),dict);
I have tried to change the variable "y" but that doesn't work or it says me that The Maximum recursion limit of 500 have been reached what can I do to fix it?

You create a dictionary for the values 1 to 256, but your text has characters with larger number representations. Using the characters you posted, you find that the minimum is 32 and the maximum is 8364. You have to create a dictionary containing all possible characters:
y = double(unique(x));
This should give the desired results. You could also use
y = 1:65535;
which will probably give you exactly the same result, as for all characters which you don't have in your text x, the probability is 0.

Related

Reading numeric data from CSV file Matlab for specific columns?

I have two .csv files which I am trying to read into Matlab as numeric matrices. Call it list_a, simply has two columns of ID numbers and corresponding values (appr. 50000 lines) with a ',' delimiter. list_b has 6 columns with a ';' delimiter. I am only interested in the first two columns containing containing numbers; the other columns contain text that I don't care about.
I initially tried using the readtable function in Matlab but noticed that these values aren't stored as numeric values, which is a requirement I have. I couldn't figure out how to cast these as integers after reading them either.
For list_a I have used the dlmread function, which I believes reads the file as numeric values.
For list_b I have tried using the dlmread function in which row and column offsets can be specified (https://www.mathworks.com/help/matlab/ref/dlmread.html#d117e329603) - the problem here is however, that the length of the file could change in the future, so I'm not sure what to enter for the row offsets.
I'm also not sure I understand how this function works, considering I tried testing it for the first 1000 rows as follows:
csv_matrix = dlmread(csv_fullpath,';',[1 1 1000 2]);
and subsequently got the following error message - even though "field number 3" shouldn't even be included in the first place:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==>
RandomTextInFile\n Error in Damage_List_Reader (line 15)
csv_matrix = dlmread(csv_fullpath,';',[1 1 1000 3]);
I get the impression that I'm making this problem a lot harder than it needs to be so if there's an all around better way to do this, I'm all ears.. Thanks!
I would suggest using fopen in combination with textscan (e.g. for list_a) like this:
file = fopen('list_a.csv');
out = textscan(file, '%d%f', 'delimiter', ',');
ID = out{1};
Vals = out{2};
'%d%f' specifies the FormatSpec, so the way how the data is formatted in file. With this, you can capture any data from a csv file (and also omit data). I recommend reading the textscan Matlab doc for further formatting issues.
P.S.: I think you can put and "end" (without the quotations) instead of one of the row offset values if the number of rows/cols isn't fixed.

Matlab string concatenation behavior with different data types

I am using Matlab 2018b on Windows 10. I understand that the Matlab string concatenation needs input arguments under concatenation to be a character array, a cell array of character vectors, or a string array as mentioned in https://de.mathworks.com/help/matlab/ref/strcat.html. Based on this i was expecting an error while trying to do the following:
[1, 'some_string']
or
strcat(1, 'some_string')
But i am not getting any error message. Instead getting output as displayed in the snapshot below:
Based on below, looks like automatic type conversion is taking place for 1 from double to char but seems something in getting wrong in the process resulting in 1 being replaced with a junk char. Any insight in to this please?
x = strcat(1, 'some_string');
class(x(1))
ans =
'char'
Matlab is treating the 1 as an ascii code, corresponding to the start of heading character which is being displayed as the square you see.
It is more obvious what Matlab is doing if you e.g. do strcat(65, 'some_string'), which returns Asome_string as the ascii code 65 corresponds to a capital A.

Issue when writing matrix to file using csvwrite

I am trying to output the matrix to a CSV file (comma separated) using this function csvwrite('myMatrix.dat',L); ( where L is square matrix) I got this error:
>> csvwrite('myMatrix.dat',L);
Error using sprintf
Function is not defined for sparse inputs.
Error in dlmwrite (line 169)
str = sprintf(format,m(i,:));
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
Kindly, what's wrong with this?
This answer was to answer OP's original error from using:
csvwrite(string('myMatrix'),L);
Error using csvwrite (line 30)
FILENAME must be a character vector.
The error you're seeing is an issue with your input (arguments). It's telling you that the file name must be a "character vector".
According to Matlab's documentation:
There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.
http://mathworks.com/help/matlab/matlab_prog/creating-character-arrays.html#briuv_1-1
In simple words. The wrong type was being used as an argument.
To provide a hint for debugging the new error.
Limitations
csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument.
csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite.
http://mathworks.com/help/matlab/ref/csvwrite.html?requestedDomain=www.mathworks.com
Try checking what's in L. whos('L') would also help you get more info on it. An easy way to view what's in your variables, is double click from the workspace. Another way is by creating a break point on your call to csvwrite within a script, then use the debugger and calling for L once you know it's loaded into memory. If you still don't know what's going on, then try 'step in' line by line.
csvwrite does not accept cell arrays.

MatLab Power Law, Non-Positive Value Error

Hi I'm trying to fit a power model to my data using MatLab's fit function
fo = fit(log2(x(:)),log2(y(:)),'power1');
plot(fo,'g'), hold on
However when I run this I get the error
Error using fit>iFit (line 282)
Cannot fit Power functions to data where X has nonpositive values.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in CurvedPowerLaw (line 20)
fo = fit(log2(x(:)),log2(y(:)),'power1');
When looking at my data and checking if any are less than 1, nothing is displayed
x(count_1)=M(i,1);
y(count_1)=M(i,2);
count_1= count_1+1;
if(M(i,2)<1)
display(M(i,1))
end;
M is a csv file with two columns. I also re ran the code for
if(M(i,1)<1)
and nothing was displayed. Checking manually and nothing seemed to be below 1 either.
i is just the line in the file that is being parsed. The file looks like
344,17
345,13
346,13
347,16
340,12
M(i,1) will result in returning one of the >300 numbers and M(i,2) will return ~10 value
Any help would be much appreciated!!
Thanks
While all values that were parsed in were >0 when scaling them by log2 that's where the 0 values started appearing. A quick fix was to add 1 to each value when parsing them in.

Error "Index exceeds matrix dimensions." in MatLab using importdata with a text file

I'm trying to import data using importdata and when I try to parse the returned data to create a matrix I get, "Index exceeds matrix dimensions". Below is my code...
traindata = importdata('textfile.txt');
%[A,delimiterOut,headerlinesOut] = importdata('textfile.txts');
disp(traindata); %everytime I run this code traindata increments by 1
X = traindata(' ',1:8); %this is where the error occurs, delimiter is 3 spaces
Y = traindata(' ',9);
Below is the format of the data in textfile.txt...
,,,5.4,,,0.0,,,0.0,,,1.6,,,2.5,,,1.0,,,6.7,,,2.8,,,6.1
,,,4.2,,,1.1,,,3.6,,,3.9,,,1.8,,,9.3,,,3.3,,,2.4,,,7.6
The data is delimited by spaces (I used commas to try and show the spaces between the data) and a newline at the end of each line. I've open textfile.txt in word and verified by viewing the hidden formatting characters. I've tried the code...
[A,delimiterOut,headerlinesOut] = importdata(inputfile);
to try to verify the delimiter used and I get the error, "Too many output arguments." As you can see I'm trying to create two matrices (X,Y) from the imported data. I've seen this specific error on stackoverflow but nothing regarding importdata. I've also tried dlmread and have not had luck. Thanks in advance for any help.
Tried the suggestion of importing the data using file->import data but I receive the error..
Error using importdata
Too many output arguments.
"Error in uiimport/runImportdata (line 433)
[datastruct, OTextDelimiter, OHeaderLines] = ...
Error in uiimport/gatherFilePreviewData (line 376)
[datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath, type);
Error in uiimport (line 194)
[ctorPreviewText, ctorHeaderLines, ctorDelim] = ..."
I'm starting to wondering if it's some sort of application bug. Here are some specifics..
"R2012a (7.0.14.739) 64 bit (Win64)". The encoding of the text file is utf-8. Thanks again for the help!
Looks like the array returned from importdata is a 1 element array.
train = importdata('textfile.txt');
fprintf('1st element in array %d\n', traindata(1)); % prints a number a number that increase each time I run this function ie 1,2,3,4...
fprintf('2nd element in array %d\n', traindata(2)); % produces error, "Index exceeds matrix dimensions"
I often find it useful to use matlab's built in GUI for importing a data file, which can help to visualised how the data will be imported. There is an option in here to produce the code required to replicate the options that were selected during the import which will allow you to work out how to dynamically import the data.
Just go to:
File >>> Import Data...