Readdlm command issue

Im new to julia and im having this issue. Im using julia version 1.4.2 running on Jupyter.
Code

using LinearAlgebra,Plots
using DelimitedFiles,SparseArrays

Retriving data in text file to an array

y = readdlm(“data.txt”);

Getting Length of array

N = length(y);

Creating ones vector of size Nx1

e = ones(N,1);

Creating SparseDaigonal matrix

D = Matrix(spdiagm(N-2, N, 0 => ones(N-2), 1 => -2*ones(N-2), 2 => ones(N-2)));

Getting Variance

Ty=(Dy)'(D*y)
println(Ty)

Creating Identity matrix of size NxN

Id = Matrix(I,N,N);

Initalsing Lamda

lam = 100;

Getting x with help of analytical Solution

x=(Id+lam*D’*D)\y;

Getting Variance after removing noise

Tx=(Dx)'(D*x);
println(Tx)

error:

at row 0, column 0 : ArgumentError(“number of rows in dims must be > 0, got 0”)
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] dlm_fill(::DataType, ::Array{Array{Int64,1},1}, ::Tuple{Int64,Int64}, ::Bool, ::String, ::Bool, ::Char) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:514
[3] readdlm_string(::String, ::Char, ::Type, ::Char, ::Bool, ::Dict{Symbol,Union{Char, Integer, Tuple{Integer,Integer}}}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:470
[4] readdlm_auto(::String, ::Char, ::Type{T} where T, ::Char, ::Bool; opts::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:246
[5] readdlm_auto at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:233 [inlined]
[6] #readdlm#5 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:170 [inlined]
[7] readdlm at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:170 [inlined]
[8] #readdlm#3 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:118 [inlined]
[9] readdlm(::String) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\DelimitedFiles\src\DelimitedFiles.jl:118
[10] top-level scope at In[3]:4

Please follow these advices to post your questions:

In particular:

  1. Wrap the example code between ```julia and ``` to make it readable in markdown.
  2. Copy the output as text, avoid screenshots.

In this case, the problem seems to be that the file is not readable byreaddlm. Can you copy the first couple of lines?

3 Likes

I am getting the same error. So is it a dependency issue?

Maybe I didn’t explain myself correctly: the advices I were commenting are for posting your example to the list and make it easier to help you, not for fixing the error.

2 Likes

This is pretty much impossible to debug, as your first line of code is y = readdlm("data.txt"), and everything follows from that - but it’s impossible for others to know what’s in data.txt and therefore what y ends up being.

Another point in the PSA was to create a minimum working example, that is a piece of code that someone else can copy/paste on their local computer and run to recreate your error. This will make it easier for others to work out what’s going wrong.

2 Likes

I think this is the error one usually finds when the file to be read is actually empty. I landed here to see if there’s a nice way to deal with these exceptions. I suppose try/catch should be my friend, but if anyone has already thought about this I’d appreciate some help :slight_smile:

Also, the OP should consider updating to the latest Julia version, Julia v1.4 is considered old for Julia standards. We are currently in Julia v1.7.

try/catch is probably the correct approach. You won’t be calling readdlm in a time-critical loop - so don’t worry about the error handling overhead.

If you want to be fancy about it, there’s always CSV.jl, which lets you do

julia> using CSV, DataFrames

julia> df = CSV.read("file.csv", DataFrame; header=["Name","Ranking","Score"], types=[String, Int64, Float64])
0×3 DataFrame

julia> df.Name
0-element PooledArrays.PooledVector{String, UInt32, Vector{UInt32}}

julia> push!(df, ("John", 24, 354.2))
1×3 DataFrame
 Row │ Name    Ranking  Score
     │ String  Int64    Float64
─────┼──────────────────────────
   1 │ John         24    354.2
1 Like

CSV is very fancy indeed! Thank you very much :slight_smile: